-2
struct Node {
int data;
struct Node *next;
};

void insertAfterHead(struct Node **a, int x) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode -> data = x;
newNode -> next = *a -> next;
*a -> next = newNode;
}


int main() {
    struct Node *head = NULL;
    struct Node *second = NULL;
    struct Node *third = NULL;

    head = malloc(sizeof(struct Node));
    second = malloc(sizeof(struct Node));
    third = malloc(sizeof(struct Node));

    head -> data = 5;
    head -> next = second;

    second -> data = 10;
    second -> next = third;

    third -> data = 15;
    third -> next = NULL;

    insertAfterHead(&head, 10);

    return 0;
}

This is the error message I get on running the code:

error: member reference base type 'struct Node *' is not a structure or union newNode -> next = *a -> next;

error: member reference base type 'struct Node *' is not a structure or union *a -> next = newNode;

Please be as descriptive as possible as I'm a beginner to C :)

Apoorve
  • 301
  • 4
  • 8
  • 3
    The error message is not corresponding to the code. And paste it here, not as an image but as a text. – Eugene Sh. Dec 13 '17 at 18:55
  • Please post your output (error message) as text, and please post the error message you got from the code you posted (not from some other code). – ikegami Dec 13 '17 at 18:56
  • In this case, there's no need to pass a pointer-to-pointer. – 001 Dec 13 '17 at 18:59
  • @EugeneSh. Sorry, I renamed the variables (for clarification) while asking. Added error message too. – Apoorve Dec 13 '17 at 19:00
  • @JohnnyMopp - Yes but I forgot the context in which I wanted to ask this so I made it up :P – Apoorve Dec 13 '17 at 19:03

1 Answers1

2
*headRef->next

means

*(headRef->next)

but you want

(*headRef)->next
ikegami
  • 367,544
  • 15
  • 269
  • 518