c - Why not use pointer to pointer when adding an element to linked list? -
i have seen question:
which uses next code add together element o list: c linkedlist inserting node @ end
int addnodebottom(int val, node *head){ //create new node node *newnode = (node*)malloc(sizeof(node)); if(newnode == null){ fprintf(stderr, "unable allocate memory new node\n"); exit(-1); } newnode->value = val; newnode->next = null; // alter 1 //check first insertion if(head->next == null){ head->next = newnode; printf("added @ beginning\n"); } else { //else loop through list , find lastly //node, insert next node *current = head; while (true) { // alter 2 if(current->next == null) { current->next = newnode; printf("added later\n"); break; // alter 3 } current = current->next; }; } homecoming 0; }
why head passed node *head
instead of node **head
if going alter within , want changes propagated outside function? missing here?
head
never modified straight in function. head->next
assigned value. hence don't need utilize pointer pointer.
c pointers
No comments:
Post a Comment