I am having problem dereferencing a double pointer (headRef) and passing the address to struct pointer (temp). In the below deleteList function when i try to pass the address of headRef->next to temp I get the error..
i.e
temp = (headRef->next);
Above statement give me the error:
<--- gives error mentioning "Request for member 'next' in something is not a struct or union..
When i try to comment out the error lines, step into deleteList function and the check the value (header->next) using gdb, i see the address of next node. But why cannot I assain the value to temp pointer. Please let me know.
The program is for deleting the entire linked list. Have pasted the program below:
Error Program:
Entire functionCode:deleteList(struct node **headRef) { struct node *temp; temp = malloc(sizeof(struct node ) ); while (*headRef != NULL) { /* Store the address of next node */ temp = (headRef->next); <--- gives error mentioning "Request for member 'next' in something is not a struct or union.. free(*headRef) *headRef = temp; } }
Code:#include <stdlib.h> #include <string.h> #include <stdio.h> struct node *BuildOneTwoThree(); void push(struct node** headRef, int newData); void deleteList(struct node **headRef); struct node { int data; struct node *next; }; int main() { struct node* myList = BuildOneTwoThree(); deleteList(&myList); return 0; } void deleteList(struct node **headRef) { struct node *temp; temp = malloc(sizeof(struct node ) ); while (*headRef != NULL) { temp = (headRef->next); free(*headRef) *headRef = temp; } } void push(struct node** headRef, int newData) { struct node* newNode = malloc(sizeof(struct node)); newNode->data = newData; newNode->next = (*headRef); *headRef = newNode; } /* Build and return the list */ struct node *BuildOneTwoThree() { struct node* head = NULL; push(&head, 3); push(&head, 2); push(&head, 1); return(head); }



LinkBack URL
About LinkBacks



