Pointer Scope

problem - problem

solution - solution in c

Why changes made in push and pop function is not followed in top method, even though I’m passing pointers to function

Please help.

I can’t see your code. Post it with public access please.

1 Like

Updated, Please have a look


As you already expected there is a problem with your pointers.

int myStackPop(MyStack* obj)
This function definition has a parameter that is a pointer to data. This means you can change the data obj is pointing to within the function but you can’t change the data region where object points to.
In the code you than assign a new data region to obj. This is only valid within your function but the caller will still only see the original data region.
For pointers that should be able to be changed you normally declare them as
MyStack ** obj. Than also the pointer given per reference and therefore changable. This will fail here because the function signature is fixed by the environment.
Other solution would be to copy all data from s2 manually to the data region object is pointing to.
Try: *obj = *s2;

1 Like

Thanks a lot!!