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

后端 未结 4 1052
情书的邮戳
情书的邮戳 2020-12-05 05:37

I want to change member of structure under double pointer. Do you know how?

Example code

typedef struct {
    int member;
} Ttype;

void changeMember         


        
相关标签:
4条回答
  • 2020-12-05 06:05

    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;
    }
    
    0 讨论(0)
  • 2020-12-05 06:17

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

    0 讨论(0)
  • 2020-12-05 06:18

    Try

    (*foo)->member = 1;
    

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

    0 讨论(0)
  • 2020-12-05 06:31

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

    (*foo)->member = 1;
    
    0 讨论(0)
提交回复
热议问题