<fk****@googlemail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
I have got these two structs where the second is embedding the first:
typedef struct{
int i1;
int i2;
}struct1;
typedef struct{
int i3;
struct1 s;
}struct2;
Then, the second struct is passed as a void pointer argument in the
callback function f:
void f(void* struct_of_type_struct2_expected){...};
My question is why I simpy can't access i2 like this:
((struct2*)struct_of_type_struct2_expected)->s.i2
This is correct and works with two compilers for me.
but have to cast s into struct1 explicitly first:
(struct1)(((struct2*)struct_of_type_struct2_expect ed)->s).i2
Is that actually what you used? This is illegal in ANSI/ISO C. You can't
cast to a struct, union, array or function.
The precedence of the direct '.' and indirect '->' component selection
operators should be the same and both left associative. If the precedence
rules are incorrect for your compiler, you'd need this (without the cast to
struct1):
(((struct2*)struct_of_type_struct2_expected)->s).i2
If you want to use the cast to struct1, it would be written like so:
((struct1*)&(((struct2*)struct_of_type_struct2_exp ected)->s))->i2
You must cast to a pointer to struct1 since casts to a struct are illegal.
Also, notice that you must take the address of struct s before casting to a
pointer to struct1 and now use the indirection operator for i2.
Rod Pemberton