pointers - Linked List in c add to front -


so i'm confused. trying write c method allows me add new "node" front of linked list. have done before in c++ , no problem. getting frustrated because after writing code pretty sure right went , looked around , everywhere find tells me same thing ready doing...i'll provide code , step step addresses , values of variables. here code:

the real function in question is:

void addtobeginning(int value, struct node* root){     struct node* newnode;     newnode = malloc( sizeof(struct node));     newnode->value = value;     newnode->next = root;     root = newnode; } 

but here complete code. removed things make more concise (things aren't necessary answer question getlength(...) , addtopos(...))

#include <stdio.h> #include <stdlib.h>  struct node {     int value;     struct node* next; };  void printlinkedlist(struct node* root);  void addtoend(int value, struct node* root);  void addtobeginning(int value, struct node* root);  void addtopos(int pos, int value, struct node* root);  int getlength(struct node* root);   int main(){             /**      testing addtobeginning (i know addtoend works)      **/      struct node *root2;     root2 = malloc( sizeof(struct node));     root2->value = 4;     root2->next = null;      /*      root2 = 4 -> 0 -> 0 -> 0 -> 0 -> 0 -> 0      */     = 0;     while (i < 5){         addtoend(0,root2);         i++;     }      printlinkedlist(root2);     //printf("length : %d\n",getlength(root2));      /*      expected root2 = 2 -> 4 -> 0 -> 0 -> 0 -> 0 -> 0      */     addtobeginning(2, root2);     printlinkedlist(root2);     /*      obtained root2 = 4 -> 0 -> 0 -> 0 -> 0 -> 0 -> 0      */     //printf("length : %d\n",getlength(root2));      return(0); }  void printlinkedlist(struct node* root){     while(root != null){         if (root->next != null){             printf("%d, ",root->value);             root=root->next;         } else {             printf("%d\n",root->value);             root=root->next;         }     }  }  void addtoend(int value, struct node* root){     /*      set new node      */     struct node* newnode = malloc( sizeof(struct node));     newnode->value = value;     newnode->next = null;      /*      check if empty linked list first      */     if (root->next == null){         root->next = newnode;     } else {         /*          find last node          */         struct node* current = root;         while(current->next != null){             current = current->next;         }         current->next = newnode;     } }  void addtobeginning(int value, struct node* root){     struct node* newnode;     newnode = malloc( sizeof(struct node));     newnode->value = value;     newnode->next = root;     root = newnode; } 

what confusing feel there problem aliasing @ the

newnode->next = root; root = newnode; 

lines...so including addresses obtained during debug steps: so...within addtobeginning(int value, struct node* root){...} function i'm going go through step step:

after execution of:

struct node* newnode; newnode = malloc( sizeof(struct node)); 

the addresses , values of root , newnode are:

root = 0x0000000100103c60 root->next = 0x0000000100103c70 root->value = 4 newnode = 0x0000000100103cc0 newnode->next = null newnode->value = 0 

after execution of:

newnode->value = value; 

the addresses , values of root , newnode are:

root = 0x0000000100103c60 root->next = 0x0000000100103c70 root->value = 4 newnode = 0x0000000100103cc0 newnode->next = null newnode->value = 2 

after execution of:

newnode->next = root; 

the addresses , values of root , newnode are:

root = 0x0000000100103c60 root->next = 0x0000000100103c70 root->value = 4 newnode = 0x0000000100103cc0 newnode->next = 0x0000000100103c60 newnode->value = 2 

after execution of:

root = newnode; 

the addresses , values of root , newnode are:

root = 0x0000000100103cc0 root->next = 0x0000000100103c60 root->value = 2 newnode = 0x0000000100103cc0 newnode->next = 0x0000000100103c60 newnode->value = 2 

i realize problem *root passed reference need change value of object stored in location 0x0000000100103c60 suggestions on how appreciated.

in function

void addtobeginning(int value, struct node* root){     struct node* newnode;     (...)     root = newnode; }  struct node *root2; addtobeginning(2, root2); 

you assign new address local root pointer. pointer has same value original root pointer copy of it. changing variable inside function has no effect on original root after function returns.

instead have pass pointer pointer:

void addtobeginning(int value, struct node **root){     struct node* newnode;     (...)     *root = newnode; }  struct node *root2; addtobeginning(2, &root2); 

additional information

it might surprising. didn't pass function pointer when wanted change original variable? well, yes, variable being pointed - not pointer itself. here, assuming original function taking pointer (not pointer pointer) have still changed original node being pointed root2 *root2 = x or members of node root2->member = y. work because change variable being pointed (even if pointers - can change these pointers [the addresses pointing to] because have pointer them , dereference pointer).

the same applies pointer variable. if want change original pointer have point , change variable being pointed to, in case original pointer.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -