syntax: struct in a struct

Discussion in 'C Programming' started by fkater@googlemail.com, Mar 23, 2006.

  1. Guest

    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
    , Mar 23, 2006
    #1
    1. Advertising

  2. Jordan Abel Guest

    On 2006-03-23, <> wrote:
    > 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?

    > but have to cast s into struct1 explicitly first:
    >
    > (struct1)(((struct2*)struct_of_type_struct2_expected)->s).i2
    >
    >
    > Felix
    >
    Jordan Abel, Mar 23, 2006
    #2
    1. Advertising

  3. Richard Bos Guest

    "" <> wrote:

    > 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
    Richard Bos, Mar 23, 2006
    #3
  4. Guest

    Jordan Abel wrote:
    > On 2006-03-23, <> wrote:


    [...]

    > > 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
    , Mar 23, 2006
    #4
  5. Guest

    Richard Bos wrote:
    > "" <> wrote:


    [...]

    > > 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).

    Felix
    , Mar 23, 2006
    #5
  6. Richard Bos Guest

    "" <> wrote:

    > Richard Bos wrote:
    > > "" <> wrote:

    >
    > > > 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).


    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
    Richard Bos, Mar 23, 2006
    #6
  7. wrote:
    > Richard Bos wrote:
    > > "" <> wrote:

    >
    > [...]
    >
    > > > 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;
    }
    Vladimir S. Oka, Mar 23, 2006
    #7
  8. Guest

    Richard Bos wrote:

    > 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
    , Mar 23, 2006
    #8
  9. <> wrote in message
    news:...
    > 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
    Rod Pemberton, Mar 23, 2006
    #9
  10. santosh Guest

    wrote:
    > Richard Bos wrote:
    >
    > > 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:


    Since your code compiles and since your construct is legal, it's
    probable that some other area of your program is trashing i2.
    santosh, Mar 23, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,356
    Chris Fogelklou
    Apr 20, 2004
  2. gabriele renzi
    Replies:
    2
    Views:
    193
    gabriele renzi
    Dec 31, 2005
  3. Ken Bloom
    Replies:
    3
    Views:
    193
  4. Good Night Moon
    Replies:
    9
    Views:
    267
    Rick DeNatale
    Jul 25, 2007
  5. Jacob Grover
    Replies:
    5
    Views:
    304
    Jacob Grover
    Jul 18, 2008
Loading...

Share This Page