namarino namarino - 23 days ago 5
C Question

Access structs through pointers

I'm working with linked lists. I'm having trouble accessing the data contained in a struct through a pointer. Here is my code:

void insertNode(Node **head, int num) {
Node *newNode = malloc(sizeof(Node));
newNode -> num = num;

while (head->next) { <-----problematic code


}


}

I've passed the address of the head pointer of the list into the function.

Node *list = malloc(arraySize * sizeof(Node));
Node *head = list;
insertNode(&head, randNum);


I keep getting an error that says "request for member "next" in something not a structure or union.

Answer

Try (*head)->next in your loop.

Reason is simple, you send Node** but expect that it will work like a Node*, which is not correct.

Comments