struct definition...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • werasm

    #1

    struct definition...

    Hi all,

    What is the difference between:

    typedef struct
    {
    ...
    } MyS1;

    ....and...

    typedef struct MyS2
    {
    ...
    } MyS2;

    Could both of the cases be instantiated as follows?

    MyS1 inst1;
    MyS2 inst2;

    Also, is the following possible...

    struct MyS3{ ... };

    MyS3 inst3;

    Kind regards,

    Werner

  • CBFalconer

    #2
    Re: struct definition...

    werasm wrote:
    >
    What is the difference between:
    >
    typedef struct
    {
    ...
    } MyS1;
    >
    ...and...
    >
    typedef struct MyS2
    {
    ...
    } MyS2;
    >
    Could both of the cases be instantiated as follows?
    >
    MyS1 inst1;
    MyS2 inst2;
    Yes. However you will not be able to instantiate a MyS1 with the
    actual name, as you can with struct MyS2 in:

    struct MyS2 foo;

    because MyS1 is the only name available for the otherwise anonymous
    struct.
    >
    Also, is the following possible...
    >
    struct MyS3{ ... };
    >
    MyS3 inst3;
    No. The name is "struct MyS3".

    --
    Chuck F (cbfalconer@yah oo.com) (cbfalconer@mai neline.net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home .att.netUSE maineline address!

    Comment

    • tjay

      #3
      Re: struct definition...

      struct doesn't create a new type which can be used like "int" or
      "float". If you just do

      struct foo {
      ...
      };

      Then anytime you want to declare a variable of that type, you'll need
      to write

      struct foo a_variable;

      Instead of just writing

      foo a_variable;

      Think of struct as a separate namespace in which foo lives.

      werasm wrote:
      Hi all,
      >
      What is the difference between:
      >
      typedef struct
      {
      ...
      } MyS1;
      The struct you defined here can only be used like this:

      MyS1 a_variable;

      This is not valid, because you defined an "anonymous" struct:

      struct MyS1 a_variable;
      >
      ...and...
      >
      typedef struct MyS2
      {
      ...
      } MyS2;
      Because you've also written the name of the struct after the "struct"
      keyword, you can now also use it like this:

      struct MyS2 a_variable;

      As well as:

      MyS2 a_variable;
      >
      Could both of the cases be instantiated as follows?
      >
      MyS1 inst1;
      MyS2 inst2;
      Yes
      >
      Also, is the following possible...
      >
      struct MyS3{ ... };
      >
      MyS3 inst3;
      That's wrong. Only built-in types and typedef'd types can be used that
      way. You need to do this:

      struct MyS3 a_variable;
      >
      Kind regards,
      >
      Werner
      To help you understand what typedef does, I've included a passage from
      The C Book [http://publications.gbdirect.co.uk/c_book/]

      "The general rule with the use of typedef is to write out a declaration
      as if you were declaring variables of the types that you want. Where a
      declaration would have introduced names with particular types,
      prefixing the whole thing with typedef means that, instead of getting
      variables declared, you declare new type names instead. Those new type
      names can then be used as the prefix to the declaration of variables of
      the new type."

      Have fun!

      TJ

      Comment

      • tjay

        #4
        Re: struct definition...

        My goodness... Google Groups chopped up my reply... Here's what I
        actually wrote:

        ----- Start real reply -----

        struct doesn't create a new type which can be used like "int" or
        "float". If you just do

        struct foo {
        ...
        };

        Then anytime you want to declare a variable of that type, you'll need
        to write

        struct foo a_variable;

        Instead of just writing

        foo a_variable;

        Think of struct as a separate namespace in which foo lives.

        werasm wrote:
        Hi all,
        >
        What is the difference between:
        >
        typedef struct
        {
        ...
        } MyS1;
        The struct you defined here can only be used like this:

        MyS1 a_variable;

        This is not valid, because you defined an "anonymous" struct:

        struct MyS1 a_variable;
        >
        ...and...
        >
        typedef struct MyS2
        {
        ...
        } MyS2;
        Because you've also written the name of the struct after the "struct"
        keyword, you can now also use it like this:

        struct MyS2 a_variable;

        As well as:

        MyS2 a_variable;
        >
        Could both of the cases be instantiated as follows?
        >
        MyS1 inst1;
        MyS2 inst2;
        Yes
        >
        Also, is the following possible...
        >
        struct MyS3{ ... };
        >
        MyS3 inst3;
        That's wrong. Only built-in types and typedef'd types can be used that
        way. You need to do this:

        struct MyS3 a_variable;
        >
        Kind regards,
        >
        Werner
        To help you understand what typedef does, I've included a passage from
        The C Book [http://publications.gbdirect.co.uk/c_book/]

        "The general rule with the use of typedef is to write out a declaration
        as if you were declaring variables of the types that you want. Where a
        declaration would have introduced names with particular types,
        prefixing the whole thing with typedef means that, instead of getting
        variables declared, you declare new type names instead. Those new type
        names can then be used as the prefix to the declaration of variables of
        the new type."

        Have fun!

        TJ

        Comment

        • werasm

          #5
          Re: struct definition...


          tjay wrote:
          Werner
          >
          To help you understand what typedef does, I've included a passage from
          The C Book [http://publications.gbdirect.co.uk/c_book/]
          >
          "The general rule with the use of typedef is to write out a declaration
          as if you were declaring variables of the types that you want. Where a
          declaration would have introduced names with particular types,
          prefixing the whole thing with typedef means that, instead of getting
          variables declared, you declare new type names instead. Those new type
          names can then be used as the prefix to the declaration of variables of
          the new type."
          >
          Have fun!
          >
          TJ
          Thank you, I'll look at the book - to summarize what you've said:

          Therefore...
          (1)
          struct ms{ };
          creates a structure identified by "ms" instantiated as follow:
          struct ms inst;

          (2)
          typedef struct ms{ } ms;
          creates a named type "ms" instantiated as follows:
          ms inst;
          ....but can also be identified by "ms" in
          struct ms inst;

          (3)
          typedef struct { } ms;
          creates an anonymous struct that can be instantiated as follows:
          ms inst;
          BTW, is this instantiable in many scopes?:

          void foo1(void){ ms inst; }
          void foo2(void){ ms inst; }

          (4)
          typedef struct ms{ };

          .... is this the same as ...

          struct ms{ };

          The reason behind my questions (I'm a C++ programmer, BTW), is that we
          want to share some C code. I therefore want to create the equivalent of
          a C++ structure in C (apart from the structure not having member
          functions).

          //C++

          struct A{ }; /*instantiatable by A a;*/

          What is the equivalent in C?

          typedef struct A{} A; /*?*/

          Kind of between languages, hope I'm OT :-).

          Eventually we want to do this (to define structures in one place and
          use in C and CPP code:

          /* c_struct.h */

          typedef struct A{ ... }A;

          //cpp_struct.hpp
          struct A_cpp : public A{ ... };

          therefore making all in A qualifiable.


          Regards,

          Werner

          Comment

          • tjay

            #6
            Re: struct definition...

            Thank you, I'll look at the book - to summarize what you've said:
            >
            Therefore...
            (1)
            struct ms{ };
            creates a structure identified by "ms" instantiated as follow:
            struct ms inst;
            Right.
            (2)
            typedef struct ms{ } ms;
            creates a named type "ms" instantiated as follows:
            ms inst;
            ...but can also be identified by "ms" in
            struct ms inst;
            Right.
            (3)
            typedef struct { } ms;
            creates an anonymous struct that can be instantiated as follows:
            ms inst;
            Right again.
            BTW, is this instantiable in many scopes?:
            >
            void foo1(void){ ms inst; }
            void foo2(void){ ms inst; }
            Yes if you defined the stuct at the top level. If it was defined in
            foo1, then foo2 would not be able to access it.
            (4)
            typedef struct ms{ };
            >
            ... is this the same as ...
            >
            struct ms{ };
            Not sure about this. I think

            typedef struct ms { };

            might not be legal, as it does not end up in a name for the typedef.

            typedef struct ms { } /* missing name here */ ;
            The reason behind my questions (I'm a C++ programmer, BTW), is that we
            want to share some C code. I therefore want to create the equivalent of
            a C++ structure in C (apart from the structure not having member
            functions).
            >
            //C++
            >
            struct A{ }; /*instantiatable by A a;*/
            >
            What is the equivalent in C?
            >
            typedef struct A{} A; /*?*/
            I think this is correct.
            This way, you can do both types of declarations in C and C++ code:

            struct A foo;
            A bar;
            >
            Kind of between languages, hope I'm OT :-).
            >
            Eventually we want to do this (to define structures in one place and
            use in C and CPP code:
            >
            /* c_struct.h */
            >
            typedef struct A{ ... }A;
            >
            //cpp_struct.hpp
            struct A_cpp : public A{ ... };
            >
            therefore making all in A qualifiable.
            I didn't even know you could inherit from a struct. You need someone
            else's opinion on this :p

            Cheers,

            TJ

            Comment

            • werasm

              #7
              Re: struct definition...


              tjay wrote:
              I didn't even know you could inherit from a struct. You need someone
              else's opinion on this :p
              This is OT, but as it answers your q. In C++ the only difference
              between a struct and a class is is its default access specifiers e.g.

              //Note: C++

              struct A
              {
              //All members are public..., if you inherit
              // from, defaults to public inheritance
              };

              class A
              {
              //All members private... defaults to private inheritance.
              };

              Thanks again for your help.

              Kind regards,

              Werner
              >
              Cheers,
              >
              TJ

              Comment

              • Chris Torek

                #8
                Re: struct definition...

                In article <1156856789.311 991.32850@p79g2 000cwp.googlegr oups.com>
                werasm <w_erasm@telkom sa.netwrote:
                >struct ms{ };
                >creates a structure identified by "ms" instantiated as follow:
                >struct ms inst;
                Yes (although there must be contents within the {}s, in C; C++ differs
                here).
                >typedef struct ms{ } ms;
                >creates a named type "ms" instantiated as follows:
                >ms inst;
                >...but can also be identified by "ms" in
                >struct ms inst;
                Essentially. However, note that it is the "struct" keyword that
                creates the type, which is named "struct ms". The "typedef",
                whose syntax includes things like:

                typedef existing_type alias_1, alias_2, alias_3;

                then creates however many aliases you list for the existing type.
                The syntax for "typedef" is identical to that for ordinary variable
                declarations, so:

                typedef int tA, tB[10], *tC, tD(void);

                defines four aliases: tA means "int", tB means "int [10]",
                tC means "int *", and tD means "int (void)". This last one is
                a function type, and can be used to declare functions but not
                to define them:

                tD foo; /* equivalent to int foo(void); */
                tD foo { return 42; } /* WRONG; diagnostic required */
                >typedef struct { } ms;
                >creates an anonymous struct that can be instantiated as follows:
                >ms inst;
                Yes: as before, the "struct" creates the type, then the subsequent
                "typedef" gives it an alias.
                >BTW, is this instantiable in many scopes?:
                >
                >void foo1(void){ ms inst; }
                >void foo2(void){ ms inst; }
                Yes. Typedef-ed identifiers obey normal scoping rules.
                >typedef struct ms{ };
                >
                >... is this the same as ...
                >
                >struct ms{ };
                It is analagous to:

                int ;

                which requires a diagnostic.
                >The reason behind my questions (I'm a C++ programmer, BTW), is that we
                >want to share some C code. I therefore want to create the equivalent of
                >a C++ structure in C (apart from the structure not having member
                >functions).
                There are a number of subtle differences here between C and C++,
                but if you want your C type-names to "work like" C++'s, do:

                /* optional: struct ms; */
                typedef struct ms ms;
                struct ms {
                ... contents ...
                };

                The initial "typedef" contains the "struct ms" part, which --
                assuming "struct ms" has not yet been defined elsewhere -- creates
                the type as an "incomplete type". The "typedef" part then gives
                it the alias. Having created both the incomplete type *and* the
                alias, you can now go on to complete the type, *and* use the alias:

                typedef struct ms ms;
                struct ms {
                ms *next;
                ... additional elements ...
                };

                See also <http://web.torek.net/torek/c/types2.html>.
                --
                In-Real-Life: Chris Torek, Wind River Systems
                Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                email: forget about it http://web.torek.net/torek/index.html
                Reading email is like searching for food in the garbage, thanks to spammers.

                Comment

                • Harald van Dijk

                  #9
                  Re: struct definition...

                  Chris Torek wrote:
                  In article <1156856789.311 991.32850@p79g2 000cwp.googlegr oups.com>
                  werasm <w_erasm@telkom sa.netwrote:
                  typedef struct ms{ };

                  ... is this the same as ...

                  struct ms{ };
                  Yes, it is.
                  It is analagous to:
                  >
                  int ;
                  >
                  which requires a diagnostic.
                  No, it isn't, and no diagnostic is required. The presence of a storage
                  class specifier ("typedef") does not imply any requirement for a
                  declarator.

                  typedef struct ms { };

                  (after adding members) is well-defined, and "typedef" is effectively
                  ignored. (I'll admit it's pointless, though.)

                  Comment

                  • Chris Torek

                    #10
                    Re: struct definition...

                    >Chris Torek wrote:
                    [an "empty" typedef, which in this case also included a non-empty
                    struct definition]
                    >... is analagous to:
                    > int ;
                    >which requires a diagnostic.
                    In article <1156915972.856 293.282840@74g2 000cwt.googlegr oups.com>
                    Harald van Dijk <truedfx@gmail. comwrote:
                    >No, it isn't, and no diagnostic is required. The presence of a storage
                    >class specifier ("typedef") does not imply any requirement for a
                    >declarator.
                    >
                    >typedef struct ms { };
                    >
                    >(after adding members) is well-defined, and "typedef" is effectively
                    >ignored. (I'll admit it's pointless, though.)
                    Oops. Indeed, no diagnostic is required in this case. I was
                    mentally mis-applying the rule that says:

                    A declaration shall declare at least a declarator (excluding
                    the parameters of a function or the members of a structure or
                    union), a tag, or the members of an enumeration.

                    Hence:

                    int; /* diagnostic required */
                    static int; /* diagnostic still required */
                    typedef; /* diagnostic required */

                    but:

                    typedef struct foo; /* diagnostic not required */
                    static enum { BAR }; /* diagnostic not required */

                    because the last two declare a tag (on the line with typedef) and
                    members of an enumeration (on the line with static).

                    (I would be unsurprised to see warnings for both of them, about
                    useless storage-class-specifier keywords, from any decent compiler.
                    But then warnings are always allowed.)
                    --
                    In-Real-Life: Chris Torek, Wind River Systems
                    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                    email: forget about it http://web.torek.net/torek/index.html
                    Reading email is like searching for food in the garbage, thanks to spammers.

                    Comment

                    Working...