Hi All,
Please see the following code for reversing a linked list
| CODE |
void rev(struct node** curr) {
node *pre =NULL;
node *q=*curr;
while(*curr!=NULL) { struct node* temp = (*curr)->link; (*curr)->link=pre;
pre=*curr; *curr=temp; } *curr=pre; } |
It works fine.....My doubt is If I put (*curr)->link=pre it works fine.........If I put
*curr->link=pre(without paranthesis) then it throws error......I don't know why.....pls explain
Thanks
| QUOTE (as_felix @ Feb 1 2007, 04:54 AM) |
My doubt is If I put (*curr)->link=pre it works fine If I put *curr->link=pre(without paranthesis) then it throws error |
The reason is that the operator -> has a higher priority that the operator *.
So in the absence of parenthesis the -> gets applied first followed by *.
So if you look at the type of curr (which is node**)
If you apply the -> operator it makes no sense and thus you get an error.
If you look at (*curr)->link now it makes sense.
The parenthesis force a different order. The * gets applied first. The type of (*curr) is node* so now the application of -> makes sense.
Hope that helps.
| QUOTE |
| then it throws error |
Please use the correct terminology.
Throwing an error implies a runtime problem and causes confusion.
What you have is a compiler error. And in the future it would be nice if you include the error with your question.