views:

1957

answers:

5

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

Example code

typedef struct {
    int member;
} Ttype;

void changeMember(Ttype **foo) {
   //I don`t know how to do it
   //maybe
   *foo->member = 1;
}

Thanks for any help.

+1  A: 

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

mepcotterell
No maybe - but dynamic allocation is immaterial.
Jonathan Leffler
+5  A: 

Try

(*foo)->member = 1;

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

JaredPar
+5  A: 

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

(*foo)->member = 1;
Konrad Rudolph
+1  A: 

Thanks for quick answer. Of course (*foo)->member is correct. I tried something like *(foo->member), (foo.member) and other bad things...

Thanks a lot.

+2  A: 

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;
}