I'm trying to implement a stack using a linked list in C but I am getting segfaults whenever I try to call the value after I push a new one onto the stack. I know this is happening because the program still says the stack is null even if I add to it. For some reason the changes that I make in the push don't stay when the function terminates and I can't figure out why.
Here's my code for the stack struct, initialization, and push:
typedef struct stack
{
int value;
struct stack * next;
} * stack_T;
stack_T
new_stack()
{
return NULL;
}
int
push_stack(stack_T s, int data)
{
stack_T new = malloc(sizeof(stack_T));
new = s;
if (s == NULL)
{
s = malloc(sizeof(stack_T));
if (s == NULL)
return 1;
}
s->value = data;
s->next = new;
return 0;
}
EDIT: Thanks for the help but I forgot to mention that the push has to be done with exactly those parameters, it's part of an assignment. I'm not looking for how to do it but rather what I'm doing wrong. I know that I can emulate pass by reference but as I said it has to be:
int push_stack(stack_T s, int data)
I've made structs before with this style and had functions that just take them as a parameter and changes stay but they won't in this case and I have no idea why.