Welcome to MSDN Blogs Sign in | Join | Help

Why are structure names different from their typedef names?

In Windows header files, many structures are declared like this:

typedef struct tagXYZ {
 ...
} XYZ;

typedef struct _XYZ {
 ...
} XYZ;

/* there are other variations, too */

Why is the structure name different from typedef name?

This is a holdover from very early versions of the C language where structure tags, union tags, and typedefs were kept in the same namespace. Consequently, you couldn't say typedef struct XYZ { ... } XYZ;. At the open brace, the compiler registers XYZ as a structure tag name, and then when XYZ appears a second time, you get a redeclaration error. The standard workaround for this was to make the structure tag name a minor modification of the typedef name, most typically by putting the word tag in front.

The C language standardization process separated the structure and typename name spaces, so this workaround is no longer necessary, but it doesn't hurt either. Besides, even if new structures followed the typedef struct XYZ { ... } XYZ; pattern, you would just have people asking, "Why do some structures in winuser.h use the tagXYZ pattern and others use the XYZ pattern? Why can't it just be consistent?"

Next time, why you also don't see the pattern typedef struct { ... } XYZ very much either.

Published Wednesday, March 26, 2008 7:00 AM by oldnewthing
Filed under:

Comments

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 10:06 AM by Coleman

Raymond,

I always thought this had more to do with name mangling in C++ than C namespaces.  

Thanks for teaching me something new!

# Chicken and the egg

Wednesday, March 26, 2008 10:14 AM by Nathan_works

This may sound like a dumb question, and the answer probably exists on the web, though I don't know the right way to phrase it as such..

Which came first, windows or MSVC (or whatever the first microsoft compiler was that contained all the windows libraries and headers) ?

I'm wondering if MS had a C compiler on hand to develop windows, or if an alternate compiler was used to develop and compile windows, and later MS developed their own compiler.

I recall that win95 and MSVC 6 were released in roughly the same time frame (as in, I had a "wow, neat, a windows 95 box!" and MSVC 6 in the same first job... )

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 10:42 AM by rlipscombe

@Nathan_works: In the past, Microsoft didn't have a compiler that was hosted on Windows. Quick C for Windows and Visual C++ 1.0 were the first.

Before that, you could use Microsoft C 6.0 to target DOS, Windows or OS/2, if I recall correctly.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 10:53 AM by Luciano

If i remember correctly (it was many years ago) the first C compiler from microsoft was produced by Lattice...

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 10:56 AM by Dave

<blockquote>Next time, why you also don't see the pattern typedef struct { ... } XYZ very much either.</blockquote>

I’m guessing this has to do with compiler error messages.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 11:05 AM by The Barefoot Bum

<i>Next time, why you also don't see the pattern typedef struct { ... } XYZ very much either.</i>

That seems trivial: you can't then have pointers to the same type of struct within the struct.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 11:22 AM by ScottD

"Next time, why you also don't see the pattern typedef struct { ... } XYZ very much either."

My guess is because you can't forward-declare a struct which has been defined that way. IIRC, it's technically a typedef to an unnamed struct.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 11:56 AM by James Curran

Next time, why you also don't see the pattern typedef struct { ... } XYZ very much either."

Mostly, because I don't believe anonomous struct have ever been legal C syntax (like a widely implemented compiler extension)

The more basic question is "why do we need the pattern typedef struct XYX { ... } XYZ; at all" (Answer, so we can refer to "struct XYZ" as just "XYZ" is a C program --- The designers of C++ wisely put that ability right into the language without needing the typedef"

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 12:07 PM by dave

"Mostly, because I don't believe anonomous struct have ever been legal C syntax (like a widely implemented compiler extension)"

Anonymous struct types (structs without a tag naming them: `struct {int a, double b} foo={42,3.14};') have been legal since at least 1989 when ANSI standardized the language; I don't have a K&R1 handy to check, but I suspect it goes even farther back than that.

You may be thinking of anonymous struct members of another struct: 'struct foo { struct {int a; double b;}; char *c;};'.  The common extension is to allow access to the members of the anonymous inner struct as 'x.a' (instead of x.inner.a), but the construct isn't legal C.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 12:14 PM by pingpong

@Nathan_works:

The compiler released around Win95 timeframe was Microsoft Visual C++ 4.0. It was followed by versions 4.1, 4.2, 5.0 and finally 6.0 in 1998.

There was no MSVC++ 3.0 AFAIR.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 12:32 PM by Jay Bazuzi

One of the annoyances with this use of typedefs is that VC++ debugger doesn't know whether to use a type's real name ("tagXYZ") or the typedef'd name ("XYZ").  Since a type can be typedef'd to multiple names, it makes sense for it to pick the canonical name, but as a user, that's not what you usually want to see.

So, why bother typdef'ing at all?  Why not just name the struct XYZ?

[Most people would have quickly tired of typing "struct" all the time. After about the tenth time of typing struct DRAWITEMSTRUCT *pdis = (struct DRAWITEMSTRUCT)lParam; you're going to say "Why can't we create a typedef for this structure?" -Raymond]

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 12:37 PM by VisualC++ x.0

>> The compiler released around Win95 timeframe was Microsoft Visual C++ 4.0.

IIRC, Win95 was published at the same time of Visual C++ 2.0 - the first true 32bit version after the hybrid 1.5. 4.0 came shortly after.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 1:01 PM by pingpong

According to http://www.emsps.com/oldtools/mscppv.htm, the MSVC 2.0 folders are dated 9/20/94 2:55am - that's almost a year before Win95 release.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 1:48 PM by Nathan_works

Thanks for the correction on which version of MSVC was released at/around win95 time -- I certainly don't have the install media around to check ;)

It was 32bit, as the guys before me had just ported it over. I was too green to deal with thunks and other 16 bit issues ...

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 4:20 PM by nikita

Struct and union field names used to live in the same global name-space too, and this is where tradition to prepend a unique prefix to struct fields is from. As a byproduct, fields weren't bound to any particular struct type:

<pre>

register *rp;

rp = p;

if(rp->i_count == 1) {

</pre>

(v6root/usr/sys/ken/iget.c:iput())

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 4:54 PM by ERock

This is the first time on Raymond's blog I've read a technical post and have known the answer before I read it.

I feel so smart.

Day after tomorrow, though, I will stop knowing the answers and will feel dumb again.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 6:41 PM by Ulric

>so this workaround is no longer necessary, but

>it doesn't hurt either.

It's an annoyance, actually, because in C++ you can't derive a class from the the typedef you're used to see.

In other words, you have to derive a c++ class from 'tagPOINT' instead of from "POINT".

I absolutely hate the 'tag' stuff in Windows header, because programmers have copied it into their code in many companies I've been at, without knowing why.  Just like the "LP" typedefs for pointers, or capital VOID.

The reason why there are typedef at all was that in "C" we had to write "struct POINT pt;"  when defining a variable.  The typedef saves us from having to type the word 'struct'.  In C++, this isn't necessary, and would be totally transparent for us if it wasn't for that 'tag' prefix, which forces us to be aware that there is a type and a typedef.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 8:04 PM by steveg

Here's a gem of an article about running Windows 1.0 under Virtual PC by Charles Petzold, which might make some oldies smile.

http://www.charlespetzold.com/etc/Windows1/index.html

I was looking around for a circa Win2.0 Windows.h file to look at structs there... stupid internet, it's like the world didn't exist before 2000.

# re: Why are structure names different from their typedef names?

Wednesday, March 26, 2008 10:05 PM by IgorD

Some compilers alow me to do this:

typedef struct _a  {

   int (*some_func_ptr)(struct _b *);

} AType;

typedef struct _b  {

...

} BType;

other compilers require this declaration up front before everything else:

struct _b;

typedef struct _a  {

   int (*some_func_ptr)(struct _b *);

} AType;

typedef struct _b  {

...

} BType;

I have no idea what is "more" legal.

# re: Why are structure names different from their typedef names?

Thursday, March 27, 2008 12:44 AM by Gabe

The Win1.0 SDK came with Microsoft C 3.0, which predates Microsoft VisualC by many years.

# re: Why are structure names different from their typedef names?

Thursday, March 27, 2008 9:27 AM by SuperKoko

"

Next time, why you also don't see the pattern typedef struct { ... } XYZ very much either.

"

Short answer: Anonymous structures don't benefit from good error/warning/debug information.

Side note:

I remember that Borland C supports something like typedef struct XYZ { ... }; as a special syntax for typedef struct XYZ { ... } XYZ;

# You got a detail wrong. e: Why are structure names different from their typedef names?

Thursday, March 27, 2008 1:04 PM by Hendrik Boom

C had one namespace for struct and union tags, but typedefs were in the same namespace as variables.  C did not automatically define a typedef name when you defined a struct.  It was C++ that introduced the implicit definition of a typedef name.

So in C people would write

 typedef struct foo { ... } foo;

and then they sould refer to the type without mentioning struct.

But when they tried compiling this in C++, the mention of "struct foo" would define the typedef name 'foo', and when the compiler got around to the 'foo' after then '}', it would complain that 'foo' had already been defined.

So to use anything like this in C++ you had to change the structure tag.

I speak from memory - I was a C user in the 70's, and a C++ implementer in the early nineties.  C++ may hae chenged then, of course.  I haven't been tracking it since then.

# re: Why are structure names different from their typedef names?

Thursday, March 27, 2008 11:28 PM by Ian Johns

# re: Why are structure names different from their typedef names?

Thursday, March 27, 2008 11:58 PM by Yuhong Bao

I think the Windows 1.0 SDK came around the time of MS C 4.0, and the Windows 2.0 SDK came around the time of MS C 5.0. Visual C++ 2.0 was released around the time of NT 3.5 (I wonder how many people believed Win95 was the first version of Windows to run Win32 apps natively). Visual C++ 4.0 was released around the release of Win95. Visual C++ 4.2 was released around the release of NT 4.0 and contained the beta of several SDKs, including the ActiveX SDK. 4.2c was released to patch them up to released versions. Visual C++ 6.0 was released around the release of Win98, but still contained the NT 4.0 headers and libraries.

# re: Why are structure names different from their typedef names?

Tuesday, April 01, 2008 12:23 PM by Alexandre Grigoriev

IgorD:

A scope for a structure/class name implicitly declared in the function declaration argument list is limited to the function declaration. It's not propagated up to the enclosing scope.

void a(class b *c); // not in the global scope

class b;

void a(class b *c); // same as in the global scope

On the other hand, a scope for a structure/class name implicitly declared in a member declaration is propagated to the enclosing scope.

class d

{

class a *b;

};

Here 'class a' belongs to the global scope, as if it were declared before class d declaration.

But if you do like this:

class d

{

class a;

class a *b;

};

Then it's class d::class a.

New Comments to this post are disabled
 
Page view tracker