namarino namarino - 1 year ago 88
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download