syntax: struct in a struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fkater@googlemail.com

    #1

    syntax: struct in a struct

    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_expecte d){...};

    My question is why I simpy can't access i2 like this:

    ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2

    but have to cast s into struct1 explicitly first:

    (struct1)(((str uct2*)struct_of _type_struct2_e xpected)->s).i2


    Felix

  • Jordan Abel

    #2
    Re: syntax: struct in a struct

    On 2006-03-23, fkater@googlema il.com <fkater@googlem ail.com> wrote:[color=blue]
    > 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_expecte d){...};
    >
    > My question is why I simpy can't access i2 like this:
    >
    > ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2[/color]

    Why can't you? What error message are you getting?
    [color=blue]
    > but have to cast s into struct1 explicitly first:
    >
    > (struct1)(((str uct2*)struct_of _type_struct2_e xpected)->s).i2
    >
    >
    > Felix
    >[/color]

    Comment

    • Richard Bos

      #3
      Re: syntax: struct in a struct

      "fkater@googlem ail.com" <fkater@googlem ail.com> wrote:
      [color=blue]
      > 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_expecte d){...};
      >
      > My question is why I simpy can't access i2 like this:
      >
      > ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2[/color]

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

      Richard

      Comment

      • fkater@googlemail.com

        #4
        Re: syntax: struct in a struct

        Jordan Abel wrote:[color=blue]
        > On 2006-03-23, fkater@googlema il.com <fkater@googlem ail.com> wrote:[/color]

        [...]
        [color=blue][color=green]
        > > My question is why I simpy can't access i2 like this:
        > >
        > > ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2[/color]
        >
        > Why can't you? What error message are you getting?[/color]

        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

        Comment

        • fkater@googlemail.com

          #5
          Re: syntax: struct in a struct


          Richard Bos wrote:[color=blue]
          > "fkater@googlem ail.com" <fkater@googlem ail.com> wrote:[/color]

          [...]
          [color=blue][color=green]
          > > My question is why I simpy can't access i2 like this:
          > >
          > > ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2[/color]
          >
          > You can. Are you sure you're using a C compiler?[/color]

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

          Felix

          Comment

          • Richard Bos

            #6
            Re: syntax: struct in a struct

            "fkater@googlem ail.com" <fkater@googlem ail.com> wrote:
            [color=blue]
            > Richard Bos wrote:[color=green]
            > > "fkater@googlem ail.com" <fkater@googlem ail.com> wrote:[/color]
            >[color=green][color=darkred]
            > > > My question is why I simpy can't access i2 like this:
            > > >
            > > > ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2[/color]
            > >
            > > You can. Are you sure you're using a C compiler?[/color]
            >
            > Hm. I am using gcc-3.4.2 (mingw-special).[/color]

            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

            Comment

            • Vladimir S. Oka

              #7
              Re: syntax: struct in a struct


              fkater@googlema il.com wrote:[color=blue]
              > Richard Bos wrote:[color=green]
              > > "fkater@googlem ail.com" <fkater@googlem ail.com> wrote:[/color]
              >
              > [...]
              >[color=green][color=darkred]
              > > > My question is why I simpy can't access i2 like this:
              > > >
              > > > ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2[/color]
              > >
              > > You can. Are you sure you're using a C compiler?[/color]
              >
              > Hm. I am using gcc-3.4.2 (mingw-special).[/color]

              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_expecte d)
              {
              ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2 = 42;
              }

              int main(void)
              {
              struct2 s2;

              f(&s2);

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

              return 0;
              }

              Comment

              • fkater@googlemail.com

                #8
                Re: syntax: struct in a struct

                Richard Bos wrote:
                [color=blue]
                > And you're compiling as C, not as, say, C++?[/color]

                Yes, I compile as C.

                [color=blue]
                > If you're compiling C, post a complete program, as small as you can cut[/color]

                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.
                [color=blue]
                > it, which exhibits the problem. It doesn't appear in my test:[/color]

                Thanks so far for testing it.

                Felix

                Comment

                • Rod Pemberton

                  #9
                  Re: syntax: struct in a struct


                  <fkater@googlem ail.com> wrote in message
                  news:1143100179 .229454.257450@ u72g2000cwu.goo glegroups.com.. .[color=blue]
                  > 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_expecte d){...};
                  >
                  > My question is why I simpy can't access i2 like this:
                  >
                  > ((struct2*)stru ct_of_type_stru ct2_expected)->s.i2
                  >[/color]

                  This is correct and works with two compilers for me.
                  [color=blue]
                  > but have to cast s into struct1 explicitly first:
                  >
                  > (struct1)(((str uct2*)struct_of _type_struct2_e xpected)->s).i2[/color]

                  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*)str uct_of_type_str uct2_expected)->s).i2

                  If you want to use the cast to struct1, it would be written like so:

                  ((struct1*)&((( struct2*)struct _of_type_struct 2_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


                  Comment

                  • santosh

                    #10
                    Re: syntax: struct in a struct

                    fkater@googlema il.com wrote:[color=blue]
                    > Richard Bos wrote:
                    >[color=green]
                    > > And you're compiling as C, not as, say, C++?[/color]
                    >
                    > Yes, I compile as C.
                    >
                    >[color=green]
                    > > If you're compiling C, post a complete program, as small as you can cut[/color]
                    >
                    > 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.
                    >[color=green]
                    > > it, which exhibits the problem. It doesn't appear in my test:[/color][/color]

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

                    Comment

                    Working...