Hari Krishna
Hari Krishna

Reputation: 551

Some clarification needed in the struct pointers

I have a doubt in the following code,

i have a function as follows,

void deleteNode(struct myList ** root)
{
  struct myList *temp;
  temp = *root;
  ...//some conditions here
  *root = *root->link;   //this line gives an error
  *root = temp->link;    //this doesnt give any error
 }

so what is the difference between the two lines, for me it looks the same.. The error is,

error #2112: Left operand of '->' has incompatible type 'struct myList * *'

Thank you :)

Upvotes: 2

Views: 572

Answers (1)

mfsiega
mfsiega

Reputation: 2872

The problem here is that the "->" operator is binding more tightly than the "*" operator. So your first statement:

// what you have written
*root->link;

is evaluating to:

// what you're getting - bad
*(root->link);

rather than:

// what you want - good
(*root)->link;

Since root is a pointer to a pointer, the -> operator doesn't make any sense on it, hence the error message.

Upvotes: 7

Related Questions