0

In the below code, I am trying to insert a node after a particular node. In the function, I will be giving as input the address of the previous node after which I want to insert the new node. The problem is in the 10th line of function insertAfter() - it says I cannot access *prev_ref->next.

#include<stdio.h>
#include<stdlib.h>

 struct node
 {
  int data;
  struct node* next;
 };

 void push(struct node **head_ref, int data)
{

struct node* newNode = (struct node*)malloc(sizeof(struct node)) ;
newNode->next= *head_ref;
newNode->data= data;
*head_ref= newNode;

}


void insertAfter(struct node **prev_ref, int data)
{
if(*prev_ref==NULL)
{
    printf("prev ref cant be null");
    return;
}
struct node * newNode;
newNode = (struct node*)malloc(sizeof(struct node)) ;
newNode->next= *prev_ref->next;
newNode->data= data;
*prev_ref->next= newNode;

}


 void printList(struct node *node)
    {
     while (node != NULL)
      {
       printf(" %d ", node->data);
       node = node->next;
    }
   }

main()
{
struct node* head = NULL;
push(&head, 7);
push(&head, 1);
insertAfter(&head, 8);
printf("\n Created Linked list is: ");
printList(head);
 getchar();
 return 0;

 }
3
  • Does (*prev_ref)->next help ? Commented Jul 1, 2014 at 8:02
  • Please paste the code for struct node and the complete compiler error message. Commented Jul 1, 2014 at 8:03
  • error: request for member 'next' in something not a structure or union. Commented Jul 1, 2014 at 9:26

2 Answers 2

1

Do you know (*p).s is equivalent to p->s ? I would suggest you to try something like (*prev_ref)->next or (**prev_ref).next

Sign up to request clarification or add additional context in comments.

1 Comment

Tried it . The concept is clear now, yet the error message remains same.
1

You seem to dereference prev_ref three-levels deep instead of two.

pointer->field is dereference of a pointer, equivalent to (*pointer).field

So, **prev_ref->next; is in fact (***prev_ref).next;

Either drop one asterisk or use . instead of ->.

EDIT: You seem to have skipped the parentheses we included in our answers.

-> has higher precedence than *.

The effect is:

(*prev_ref)->next

  • first uses '*' and finds the memory pointed to by prev_ref (let's call it memory location A),
  • then uses '->' to find memory pointed to by A, let's call it B,
  • then location of the the next field of the structure, offset by a set distance from B, let's call it C
  • and finally accesses (reads/writes) the value stored at C.

Now for *prev_ref->next

  • first, uses -> and finds the memory pointed to by prev_ref (A), just the same
  • then the location of the the next field of the structure, offset by a set distance from A, which happens to be an entirely random location in memory (because A stored a pointer to the structure, not the structure itself); let's call that location D.
  • Then it tries to find the memory location at wherever D pointed to, which is entirely random.

Now, the system won't let you do that, because it sees A is not where a structure lies, but where a pointer to a structure lies, hence the error message

And the fundamental reason of your problems is that you use pointers-to-pointers for no good reason. Nothing of this would have happened if you always used plain pointers. void push(struct node *head_ref, int data) , void insertAfter(struct node *prev_ref, int data), prev_ref->next etc. Managing pointers to pointers is tricky, error-prone (as you've experienced) and in 99% cases completely unnecessary.

1 Comment

Thanks for clearing the concept . But still i get the same error message. Hence i have pasted the entire code . Also i have given the error message in comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.