By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,959 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,959 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Aug 3 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
GB
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
Aug 3 '05 #2

P: n/a
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

Aug 3 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.