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

  • Thread starter Steven T. Hatton
  • Start date
S

Steven T. Hatton

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 {/*...*/};?
 
G

GB

Steven said:
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
 
G

Greg

Steven said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top