Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typedef struct {} SName; vs. struct SName{};

Reply
Thread Tools

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

 
 
Steven T. Hatton
Guest
Posts: n/a
 
      08-03-2005
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
 
Reply With Quote
 
 
 
 
GB
Guest
Posts: n/a
 
      08-03-2005
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
 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      08-03-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
Confusing Typedef/Struct, What Does This Do? j6vflbl6vy6g8o001@sneakemail.com C++ 2 02-17-2005 11:36 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments