简体   繁体   中英

de referencing a structure variable in C

I have two structures like this

typedef struct foo_ {
int id;
int var1;
int var2; 
} foo_t;

typedef struct bar_ {
int member1;
int member2;
foo_t an_array[10]; 
} bar_t;

I have a pointer to the structure bar_ as bar_t *ptr

Now i need to access the id in foo

I get it as ptr->an_array[index].id Since it is an integer, there won't be any problem. But how do I pass the the variable id by reference?

&ptr->an_array[index].id doesn't seem to work nor does &ptr->(an_array[index].id)

I went through the precedences but still can't get it working.

Can someone please help me with this

Check your code again - &ptr->an_array[index].id should work just fine. Postfix operators ( -> , [] , . ) bind tighter than & , so there should be no problem. Maybe there is another issue in your original code?

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