简体   繁体   中英

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;

我怀疑您需要将指针传递给rootA变量,而不是对其进行两次取消引用。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM