typedef struct {} SName; vs. struct SName{};

Discussion in 'C++' started by Steven T. Hatton, Aug 3, 2005.

  1. typedef struct {
    unsigned char e_ident[EI_NIDENT];
    Elf32_Half e_type;
    Elf32_Half e_machine;
    Elf32_Word e_version;
    Elf32_Addr e_entry;
    Elf32_Off e_phoff;
    Elf32_Off e_shoff;
    Elf32_Word e_flags;
    Elf32_Half e_ehsize;
    Elf32_Half e_phentsize;
    Elf32_Half e_phnum;
    Elf32_Half e_shentsize;
    Elf32_Half e_shnum;
    Elf32_Half e_shstrndx;
    } Elf32_Ehdr;

    I came across the above structure in "Tool Interface Standard (TIS)
    Executable and Linking Format (ELF) Specification Version 1.2". Is there
    any difference between using that form of creating a struct, and the
    "official" C++ way of `struct name {/*...*/};?
    --
    If our hypothesis is about anything and not about some one or more
    particular things, then our deductions constitute mathematics. Thus
    mathematics may be defined as the subject in which we never know what we
    are talking about, nor whether what we are saying is true.-Bertrand Russell
    Steven T. Hatton, Aug 3, 2005
    #1
    1. Advertising

  2. Steven T. Hatton

    GB Guest

    Steven T. Hatton wrote:
    > typedef struct {
    > unsigned char e_ident[EI_NIDENT];
    > Elf32_Half e_type;
    > Elf32_Half e_machine;
    > Elf32_Word e_version;
    > Elf32_Addr e_entry;
    > Elf32_Off e_phoff;
    > Elf32_Off e_shoff;
    > Elf32_Word e_flags;
    > Elf32_Half e_ehsize;
    > Elf32_Half e_phentsize;
    > Elf32_Half e_phnum;
    > Elf32_Half e_shentsize;
    > Elf32_Half e_shnum;
    > Elf32_Half e_shstrndx;
    > } Elf32_Ehdr;
    >
    > I came across the above structure in "Tool Interface Standard (TIS)
    > Executable and Linking Format (ELF) Specification Version 1.2". Is there
    > any difference between using that form of creating a struct, and the
    > "official" C++ way of `struct name {/*...*/};?


    The header may be intended to be included not only from a C++ program,
    but also from a C program. In C, the struct tag cannot be used as a type
    name without preceding it with the keyword "struct":

    typedef struct Tag {
    int field;
    } Type;

    Tag var1; /* Valid in C++, not in C */
    struct Tag var2; /* Valid in both C and C++ */
    Type var3; /* Valid in both C and C++ */

    Gregg
    GB, Aug 3, 2005
    #2
    1. Advertising

  3. Steven T. Hatton

    Greg Guest

    Steven T. Hatton wrote:
    > typedef struct {
    > unsigned char e_ident[EI_NIDENT];
    > Elf32_Half e_type;
    > Elf32_Half e_machine;
    > Elf32_Word e_version;
    > Elf32_Addr e_entry;
    > Elf32_Off e_phoff;
    > Elf32_Off e_shoff;
    > Elf32_Word e_flags;
    > Elf32_Half e_ehsize;
    > Elf32_Half e_phentsize;
    > Elf32_Half e_phnum;
    > Elf32_Half e_shentsize;
    > Elf32_Half e_shnum;
    > Elf32_Half e_shstrndx;
    > } Elf32_Ehdr;
    >
    > I came across the above structure in "Tool Interface Standard (TIS)
    > Executable and Linking Format (ELF) Specification Version 1.2". Is there
    > any difference between using that form of creating a struct, and the
    > "official" C++ way of `struct name {/*...*/};?
    > --


    There is a difference between the two forms. The C declaration in the
    example above declares an unnamed type of struct and then declares a
    typedef named "Elf32_Ehdr" that can be used to refer to it.

    The C++ declaration:

    struct Elf32_Ehdr {
    ....
    };

    is much more straightforward. It declares a type of struct named
    "Elf32_Ehdr".

    The disadvantage with the typedef name is that it appears to be a
    typename in the source code, but it is not in fact the name of a
    declared type. Elf32_Ehdr will not appear in a list of the program's
    class types.

    A typedef name is a weaker way of naming than the actual type name.
    It's a bit like trying to get a drivers license under a nickname. You
    can go by a nickname most of the time, but now and then you have to use
    your real name. By making the nickname and the real name the same, you
    can avoid that kind of problem.

    Greg
    Greg, Aug 3, 2005
    #3
    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. DanielEKFA
    Replies:
    8
    Views:
    601
    DanielEKFA
    May 16, 2005
  2. Amit Khemka
    Replies:
    0
    Views:
    654
    Amit Khemka
    Sep 21, 2007
  3. oor
    Replies:
    0
    Views:
    1,341
  4. Rivka Miller

    Can someone give me an example of the SNAME type ?

    Rivka Miller, Jun 30, 2010, in forum: C Programming
    Replies:
    1
    Views:
    271
    Rivka Miller
    Jul 1, 2010
  5. Rivka Miller
    Replies:
    2
    Views:
    288
    James Kanze
    Jul 4, 2010
Loading...

Share This Page