Hello everybody,
http://www.cprogramming.com/tutorial/c/lesson15.html
in the sample code of that Tutorial, which shows how one can traverse a linked list, there are some things I don't understand. Here is the part of the code. I've added a comment to the line I don't understand:
Why is root->next set to a null pointer? Isn't root->next a null pointer by default?Code:/*...*/ struct node { int x; struct node *next; }; int main() { /* This won't change, or we would lose the list in memory */ struct node *root; /* This will point to each node as it traverses the list */ struct node *conductor; root = malloc( sizeof(struct node) ); root->next = 0; /* QUESTION: Why giving root->next a null pointer? */ root->x = 12; conductor = root; if ( conductor != 0 ) { while ( conductor->next != 0) { conductor = conductor->next; } } /*...*/ }
Furthermore, there are 2 things I don't understand:
1. What is the use for "if ( conductor != 0 )" when they just wrote conductor = root before?
2. What is the goal of "while ( conductor->next != 0)"? If you just wrote "root->next = 0; conductor = root;" then is "conductor->next" in any case also 0! So the checking "while ( conductor->next != 0)" doesn't make any sense, because it's always 0.