这里我通过值方法传递一个指针 struct stack * sp 那么值如何在堆栈代码中修改?

Here I pass a pointer struct stack * sp by value approach then how value get modify in stack code?

在这段代码中,我将 struct stack * sp 作为按值调用传递,如果它是按值传递方法,那么它是如何更新函数中的值的,如果我遗漏了什么,请帮忙找出答案?

#include<stdio.h>    
#include<conio.h>
         
struct stack{
    int size ;
    int top;
    int * arr;
};//creating structures 

void traversal(struct stack *sp){
    for(int i=0;i<=sp->top;i++){
        printf("%d\n",sp->arr[i]);
    }
}
 
int isEmpty(struct stack* ptr){
    if(ptr->top == -1){
            return 1;
        }
        else{
            return 0;
        }
}
 
int isFull(struct stack* ptr){
    if(ptr->top == ptr->size - 1){
        return 1;
    }
    else{
        return 0;
    }
}
 
void push(struct stack* ptr, int val){
    if(isFull(ptr)){
        printf("Stack Overflow! Cannot push %d to the stack\n", val);
    }
    else{
        ptr->top=ptr->top+1;//how it gets update when i pass by value
        ptr->arr[ptr->top] = val;
    }
}
 
int pop(struct stack* ptr){
    if(isEmpty(ptr)){
        printf("Stack Underflow! Cannot pop from the stack\n");
        return -1;
    }
    else{
        int val = ptr->arr[ptr->top];
        ptr->top=ptr->top-1;//ptr pass by value how it gets update 
        return val;
    }
}
 
int main(){
    **struct stack *sp** = (struct stack *) malloc(sizeof(struct stack));//local pointer variable
        sp->size = 10;
        sp->top = -1;
        sp->arr = (int *) malloc(sp->size * sizeof(int));
        printf("Stack has been created successfully\n");
        push(sp,10);//pass by value in push
        push(sp,200);//pass by value in push
        traversal(sp);    
        printf("the %d element from stack is popped\n",pop(sp));
        printf("the %d element from stack is popped\n",pop(sp));
        traversal(sp);
    return 0;
}

现在在这段代码中,push 和 pop 修改了顶部和数组,就像我说的那样,我是按值调用传递的,但是它们也在上面的代码中被修改了,但是如何修改?

您的代码似乎包含一些错误,所以让我先解决这些错误:

  1. 您在 main() 函数中声明了这个 **struct stack *sp** = (struct stack *) malloc(sizeof(struct stack))。这是错误的,会报错。您似乎想按值传递 sp,声明 sp 的正确方法是 struct stack *sp = (struct stack *) malloc(sizeof(struct stack)).

您似乎主要对 sp 指向的结构如何被您为实现堆栈所做的函数修改感到困惑,让我们通过 main() 看看发生了什么.

main():

int main(){
    
struct stack *sp = (struct stack *) malloc(sizeof(struct stack));//local pointer variable
       
sp->size = 10;
sp->top = -1;
sp->arr = (int *) malloc(sp->size * sizeof(int));


printf("Stack has been created successfully\n");
 
push(sp,10);//pass by value in push
push(sp,200);//pass by value in push
      
traversal(sp);

printf("the %d element from stack is popped\n",pop(sp));      
printf("the %d element from stack is popped\n",pop(sp));

traversal(sp);

   return 0;
}

所以我将分解它是如何工作的:

首先在 main() 中我们有一个指向类型 struct stack:

的结构的指针

struct stack *sp = (struct stack *) malloc(sizeof(struct stack));

此结构已为其分配了一些内存,sp 存储了此结构的地址。

接下来在 main() 中,您将 10 存储在 size 成员中 sp 指向的结构中:

sp->size = 10;

您还将 -1 存储在 sp 指向的结构中名为 top 的成员中:

sp->top = -1;

最后,您在这一行中为堆栈分配了一些内存,在本例中,您为 10 个整数分配了 40 bytes

sp->arr = (int *) malloc(sp->size * sizeof(int));

现在转到函数调用:

你写了push(sp,10)

让我们进一步分解并查看 push()

push():

void push(struct stack* ptr, int val){

        if(isFull(ptr)){
            printf("Stack Overflow! Cannot push %d to the stack\n", val);
        }

        else{
            ptr->top=ptr->top+1;//how it gets update when i pass by value
            ptr->arr[ptr->top] = val;
        }
    }

push()有2个参数,一个是指向结构的指针,第二个是整数值。

push(sp,10)

现在你将 sp 传递给第一个参数 struct stack* ptr,记住 sp 保存了你在 main() 中为其分配内存的结构的地址。

您现在将此地址提供给 struct stack* ptr,它是指向 push() 函数的本地指针。 push()结束后,struct stack* ptr会被销毁。

考虑到这一点,你正在按值传递 sp 因为你实际上并没有传递 sp 本身(sp 的地址),你传递的是 sp 指向的地址to/holding 复制成 struct stack* ptr.

现在因为你传递了sp也是holding/pointing的结构的地址,这个地址可以被修改。