-1
void push(struct node* head,int data)
{
    struct node* newnode=(struct node*)malloc(sizeof(struct node)); 
    newnode->data=data;
    newnode->next=head;
    head=newnode; //it doesnt work
}

This code is not working; the error that occured was not a syntax error.

rgettman
  • 176,041
  • 30
  • 275
  • 357
77981
  • 9
  • 2
  • So is it a syntax error or not a syntax error? You need to declare the parameter as `struct node** head` and change the last statement to `*head=newnode`. – Alex Shesterov Mar 29 '16 at 21:38
  • the change to `head` won't be visible outside the `push` function, and I presume that's exactly what you'd like. – isedev Mar 29 '16 at 21:38
  • code is not working means a lot, please point out your error. – Dilip Kumar Mar 29 '16 at 21:38
  • the code is taken from a book..where the writer assumes that this code doesnt work – 77981 Mar 29 '16 at 21:40
  • @isedev why it wont be visible ? – 77981 Mar 29 '16 at 21:41
  • @77981 All function parameters are passed by value in C. So the `head` variable within the `push` function is not the same `head` variable that the caller passed in (it's just the same value). So changing one does not affect the other. The variable needs to be passed in as `struct node **head;` and set with `*head=newnode;`. – kaylum Mar 29 '16 at 21:43
  • A syntax error means it doesn't compile, and as a result you can't even attempt to run it. It sounds instead like this is a functional error. The last statement, where you assign to `head`, is dead code since it has no effect after the function returns. `head` is just a function argument, which is basically a type of local variable that is bound upon entry to the function. You probably intended to pass a *pointer* to head so that you can change it in the caller. – Tom Karzes Mar 29 '16 at 21:43
  • see [C: change pointer passed by value](http://stackoverflow.com/questions/5531765/c-change-pointer-passed-by-value) – isedev Mar 29 '16 at 21:44
  • if the function is not void,and it returns the variable head,does it work ? – 77981 Mar 29 '16 at 21:48
  • Yes that is another way to do it. – kaylum Mar 29 '16 at 21:56

1 Answers1

0

Parameters in C are passed by value, rather than passed by reference, that is, formal parameters are just copies of actual parameters. Maybe you can understand why the following function doesn't work:

void negate(int num)
{
    num = -num; // Doesn't work
}

The reason why the posted code doesn't work is just similar. head = newnode; only changes the value of formal parameter head. After push() returns, the value of actual parameters from the caller remain unmodified. Thus, for the caller, nothing happens(expect that a block of memory is malloc()ed but not free()ed).

To change the value of variables, you must pass their address to functions:

void push(struct node **head,int data)
{
    struct node* newnode = malloc(sizeof(struct node)); 
    newnode->data = data;
    newnode->next = head;
    *head = newnode;
}

See also: How do I modify a pointer that has been passed into a function in C?

Community
  • 1
  • 1
nalzok
  • 14,965
  • 21
  • 72
  • 139