views:

85

answers:

2

I've got a structure which holds names and ages. I've made a linked-list of these structures, using this as a pointer:

aNode *rootA;

in my main. Now i send **rootA to a function like so

addElement(5,"Drew",&rootA);

Because i need to pass rootA by reference so that I can edit it in other functions (in my actual program i have two roots, so return will not work)

The problem is, in my program, i can't say access the structure members.

*rootA->age = 4;

for example doesnt work.

Hopefully you guys can help me out. Thanks!

+6  A: 

It's hard to tell from your question but it looks like the type of rootA in the last sample is aNode**. If so the reason why it's failing is that -> has higher precedence than *. You need to use a paren to correct this problem

(*rootA)->age = 4;

A full precedence table is available here

If the type of rootA is instead aNode*. Then you don't need to dereference in addition to using ->. Instead just use -> directly

rootA->age = 4;
JaredPar
Wow, that does it perfectly. Thanks, i had no idea it worked that way. Also, getting an answer in 36 seconds thats completely right is awesome. I have to wait 11min to select it as the right answer, lol!
Blackbinary
+1 for a very complete answer
Sam Post
Precedence mistakes are common faults in today's programming.
Robert
A: 

I suspect you need to pass a pointer to the rootA variable, and not dereference it twice.

addElement(5,"Drew",&rootA);
Donal Fellows
my copying abilities are to blame, fixed in the question.
Blackbinary