Accessing structure elements via double pointers in C

一个人想着一个人 提交于 2021-02-19 04:19:11

问题


I'm implementing linked lists using structures. I have a structure -

typedef struct llist node;
typedef node *nodeptr;
struct llist
{
    int data;
    nodeptr next;
};

Now lets say I declare a variable nodeptr *ptr;. How do I access the members data and next using ptr?


回答1:


You deference the first pointer and then the second one.

To access the data and next in the structure statement would like this

(*ptr)->data = 5;
(*ptr)->next = temp;

brackets around ptr is required since -> has higher priority than *.

-> is equivalent to writing *. (e.g. ptr->data is the same as *ptr.data).



来源:https://stackoverflow.com/questions/21398105/accessing-structure-elements-via-double-pointers-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!