I am getting a segmentation fault in the following code:
void print_stack(Node * root) {
while(root != NULL) {
// print the node
root = root->next;
}
}
Whereas this works:
int print_stack(Node ** root) {
Node * tmp = *root;
while(*root != NULL) {
// print the node
*root = (*root)->next;
}
*root = tmp;
}
The question is what am I doing wrong? For both functions I am passing the address of a Node pointer to the head of the list. I am trying to get the first function to work because it seems more ideal (no pointer allocation and no permanent change to root pointer).. thanks.
EDIT: I have posted the code here: http://dpaste.com/477724/
You passed the address of a Node pointer while the function takes just a Node pointer.
This:
should be this: