How to work with pointer to pointer to structure in C?

最后都变了- 提交于 2019-11-27 20:17:33
JaredPar

Try

(*foo)->member = 1;

You need to explicitly use the * first. Otherwise it's an attempt to dereference member.

Due to operator precedence, you need to put parentheses around this:

(*foo)->member = 1;

You can use a temp variable to improve readability. For example:

Ttype *temp = *foo;
temp->member = 1;

If you have control of this and allowed to use C++, the better way is to use reference. For example:

void changeMember(Ttype *&foo) {
   foo->member = 1;
}

maybe (*foo)->member = 1 (if it's dynamically allocated)

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