Reputation: 179
I have to implement a simple stack in C but it has been a few years since I wrote something in C so am a bit rusty.
I have a struct defined in the .h file like this, typedef struct _my_stack my_stack_t;
as well as a function that initializes the stack my_stack_t* my_stack_new();
and a push function void my_stack_push(my_stack_t *s, void *data);
in the .c file i defined the stack and the init function like this
struct _my_stack{
void* data;
struct _my_stack* next;
};
my_stack_t* my_stack_new(){
my_stack_t* mystack = NULL;
return mystack;
};
then i defined the push function like this:
void my_stack_push(my_stack_t *s, void *data){
my_stack_t* mystack = (my_stack_t*)malloc(sizeof(my_stack_t));
mystack->data = data;
mystack->next = s;
s=mystack;
};
however the push does not seem to work, is i get segmentation faults when trying to access the data of the element i pushed. so why does it not work. in the push it allocated the space for the stack item and puts the pointer to the data in the data variable. and it takes the pointer to the current head of the stack and puts it in the next var and then makes the stackhead point the current element.
note: the .h file is given, so i have to take functions are they are declared there.
Upvotes: 0
Views: 161
Reputation: 44370
The problem is that changes to s
inside my_stack_push
doesn't change the value of the variable used as argument when my_stack_push
was called.
If your code is:
int main()
{
...
my_stack_t* s = my_stack_new();
my_stack_push(s, some_data_pointer);
...
}
then the s
in main
is a different variable than s
inside my_stack_push
. The only relation they have is that s
inside my_stack_push
is initialized with a copy of the value of s
in main
.
Consequently - changing the value of s
inside my_stack_push
does not change the value of s
in main
.
To change the variable outside the function you need to pass the function a pointer to that variable.
So what you want is:
void my_stack_push(my_stack_t **s, void *data){ // Notice the extra *
my_stack_t* mystack = malloc(sizeof(my_stack_t));
mystack->data = data;
mystack->next = *s; // Notice the extra *
*s=mystack; // Notice the extra *
};
And use it like:
my_stack_t* s = my_stack_new();
my_stack_push(&s, some_data_pointer); // Notice the &
Upvotes: 2
Reputation: 32594
however the push does not seem to work, is i get segmentation faults when trying to access the data of the element i pushed. so why does it not work
probably you do something like that :
my_stack_t* mystack;
my_stack_push(mystack, ..adata..);
supposing mystack is modified by my_stack_push
, this is not the case because in my_stack_push
the form s=mystack;
just modify the local variable
You have two ways to change that
1) give the address of the var in parameter rather than its value
my_stack_t* mystack = NULL;
my_stack_push(&mystack, ..adata..);
and of course
void my_stack_push(my_stack_t **s, void *data){
my_stack_t* mystack = (my_stack_t*)malloc(sizeof(my_stack_t));
mystack->data = data;
mystack->next = *s;
*s=mystack;
};
2) return the new cell
my_stack_t* mystack = NULL;
mystack = my_stack_push(mystack, ..adata..);
and of course
my_stack_t * my_stack_push(my_stack_t *s, void *data){
my_stack_t* mystack = (my_stack_t*)malloc(sizeof(my_stack_t));
mystack->data = data;
mystack->next = s;
return mystack;
};
Upvotes: 1