different struct

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

    different struct

    what's the difference when declare a struct with typedef or NO
    typedef?

    =============== =============== ==
    typedef struct {

    .... etc

    } SetupRecord;

    =============== =============== ==
    struct {

    .... etc

    } SetupRecord;
  • Ian Collins

    #2
    Re: different struct

    Jim Johnson wrote:
    what's the difference when declare a struct with typedef or NO
    typedef?
    >
    =============== =============== ==
    typedef struct {
    >
    .... etc
    >
    } SetupRecord;
    >
    This defines a type in C.
    =============== =============== ==
    struct {
    >
    .... etc
    >
    } SetupRecord;
    this does not.

    Only use the first form if you are sharing types between C and C++.

    The second form is unusual, use

    struct SetupRecord {...};

    --
    Ian Collins.

    Comment

    • Jim Johnson

      #3
      Re: different struct

      So there are 2 ways to declare a struct

      1) C way of declaring
      typedef struct {
      >
      .... etc
      >
      } SetupRecord;
      2) C++ way of declaring a struct
      struct SetupRecord {
      >
      .... etc
      >
      } ;
      may I correct?

      and also the following is incorrect synatx to declare a struct
      struct {
      >
      .... etc
      >
      } SetupRecord;

      Comment

      • Jack Klein

        #4
        Re: different struct

        On Mon, 10 Mar 2008 21:26:12 -0800, Jim Johnson <aopiyy001@yaho o.com>
        wrote in comp.lang.c++:
        So there are 2 ways to declare a struct
        >
        1) C way of declaring
        No, this is not declaring anything, this is defining a type, and also
        creating an alias for it.
        typedef struct {

        .... etc

        } SetupRecord;
        This is also perfectly valid C++. It defines a structure type with no
        name, and defines SetupRecord as an alias for that type. This is
        actually not a particularly good idea in either language.
        2) C++ way of declaring a struct
        Again, your sample below does not declare a struct, it defines a
        struct type.
        struct SetupRecord {

        .... etc

        } ;
        This is perfectly valid C does as well. In both languages, it defines
        a structure type named SetupRecord. In either language, the type can
        be referred to by the two-word phrase "struct SetupRecord". In C++
        only, it can also be referred to merely as SetupRecord.
        may I correct?
        >
        and also the following is incorrect synatx to declare a struct
        Your terminology is still incorrect. Your snippet below defines a
        structure type, and also defines an object of that structure type.
        struct {

        .... etc

        } SetupRecord;
        You have defined an unnamed structure type, and an instance of that
        type named SetupRecord. In this situation, SetupRecord is the name of
        a single object of this type, and not of the structure type itself.

        It is perfectly valid code, and this object can be accessed anywhere
        within its scope. Compile and execute the following example:

        #include <iostream>

        struct { int x; int y; } ss;

        void show_me()
        {
        std::cout << ss.x << " + " << ss.y << " = "
        << (ss.x + ss.y) << std::endl;
        }

        int main()
        {
        ss.x = 2;
        ss.y = 3;
        show_me();
        return 0;
        }

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://c-faq.com/
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        • Tim Slattery

          #5
          Re: different struct

          Jim Johnson <aopiyy001@yaho o.comwrote:
          >what's the difference when declare a struct with typedef or NO
          >typedef?
          >
          >============== =============== ===
          >typedef struct {
          >
          .... etc
          >
          >} SetupRecord;
          This defines SetupRecord to be a type, which corresponds to this
          structure. You can define other items of type SetupRecord:

          SetupRecord xyz;

          "xyz" is now a variable of type SetupRecord.
          >============== =============== ===
          >struct {
          >
          .... etc
          >
          >} SetupRecord;
          This defines a variable named SetupRecord. It's a structure whose
          properties you've just defined.

          --
          Tim Slattery
          Slattery_T@bls. gov

          Comment

          • Ron Natalie

            #6
            Re: different struct

            Ian Collins wrote:
            Jim Johnson wrote:
            >what's the difference when declare a struct with typedef or NO
            >typedef?
            >>
            >============== =============== ===
            >typedef struct {
            >>
            > .... etc
            >>
            >} SetupRecord;
            >>
            This defines a type in C.
            >
            >============== =============== ===
            >struct {
            >>
            > .... etc
            >>
            >} SetupRecord;
            >
            this does not.
            >
            Only use the first form if you are sharing types between C and C++.
            >
            The second form is unusual, use
            >
            struct SetupRecord {...};
            >
            The latter doesn't define a type at all, it defines a variable of
            unnamed type called SetupRecord.

            Comment

            Working...