syntax: struct in a struct

F

fkater

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

but have to cast s into struct1 explicitly first:

(struct1)(((struct2*)struct_of_type_struct2_expected)->s).i2


Felix
 
J

Jordan Abel

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

Why can't you? What error message are you getting?
 
R

Richard Bos

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

You can. Are you sure you're using a C compiler?

Richard
 
F

fkater

Jordan said:
[...]
My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2

Why can't you? What error message are you getting?

Unfortunatelly I don't get an error message. It just turned out that i2
never contained the expected results, and explicitly casting s before
surrounded this problem.

Felix
 
R

Richard Bos

Hm. I am using gcc-3.4.2 (mingw-special).

And you're compiling as C, not as, say, C++? There are areas surrounding
conversion where C++ makes different demands from C; I do not know
whether this is one of them.
If you're compiling C, post a complete program, as small as you can cut
it, which exhibits the problem. It doesn't appear in my test:

#include <stdio.h>

typedef struct {
int i1;
int i2;
} struct1;

typedef struct {
int i3;
struct1 s;
} struct2;

void f(void *str2)
{
printf("%d\n", ((struct2 *)str2)->s.i2);
return;
}

int main(void) {
struct2 s2={3, {1,2}};

f(&s2);

getchar();
return 0;
}

I get an output of 2, as expected. This with Dev-C++, which is also gcc
plus MinGW.

Richard
 
V

Vladimir S. Oka

Richard said:
[...]
My question is why I simpy can't access i2 like this:

((struct2*)struct_of_type_struct2_expected)->s.i2

You can. Are you sure you're using a C compiler?

Hm. I am using gcc-3.4.2 (mingw-special).

I just tried it on exact same compiler, and it works.

Here's the exact code I tried:

#include<stdio.h>

typedef struct {
int i1;
int i2;
} struct1;

typedef struct{
int i3;
struct1 s;
} struct2;

void f(void* struct_of_type_struct2_expected)
{
((struct2*)struct_of_type_struct2_expected)->s.i2 = 42;
}

int main(void)
{
struct2 s2;

f(&s2);

printf("%d\n", s2.s.i2);

return 0;
}
 
F

fkater

Richard said:
And you're compiling as C, not as, say, C++?

Yes, I compile as C.

If you're compiling C, post a complete program, as small as you can cut

Hm, I'll see what I can extract but this seems part of the problem
itself since the code I posted seems to be the relevant one as far as I
unterstand things here. So, it will take a little to extract the right
code from my program (which is quite large) to still generate the
error.
it, which exhibits the problem. It doesn't appear in my test:

Thanks so far for testing it.

Felix
 
R

Rod Pemberton

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_expected)->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_expected)->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
 
S

santosh

Yes, I compile as C.



Hm, I'll see what I can extract but this seems part of the problem
itself since the code I posted seems to be the relevant one as far as I
unterstand things here. So, it will take a little to extract the right
code from my program (which is quite large) to still generate the
error.

Since your code compiles and since your construct is legal, it's
probable that some other area of your program is trashing i2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top