![]() To avoid complexity of double pointers we can always utilize structure(which works as an internal pointer). Pointer to pointer(Double pointers) also allows for creation for multiple user defined data types within a same program(Example: Creating 2 linked lists) The advantage should be clear now: with the second, if the caller forgets to assign the returned node to the head pointer, bad things will happen. If you don't do this assignment when calling this function, you will be leaking the nodes you allocate with malloc, and the head pointer will always point to the same node. note the assignment of the result to the head pointer The implementation that doesn't take a pointer to the head pointer must return the new head, and the caller is responsible for updating it itself: struct node* push(struct node* head, int data) The two pointer jump is traversing the entire thing, to figure out how to jump twice it jumps once to get the next pointer then jumps again. *head = newnode // *head stores the newnode in the head Double-pointers are used as arguments of function when the function modifies head of the linked list (updates the list) without needing to return the head. ![]() ![]() Struct node* newnode = malloc(sizeof(struct node)) Thus you could write: // note that there's no return value: it's not needed Some implementations pass a pointer to pointer parameter to allow changing the head pointer directly instead of returning the new one.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |