开发者

Accessing structure through pointers [c]

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!


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;

See full C Operator Precedence Table.

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;


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜