# struct / typedef question

This topic is 4891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I've always wondered about this but never bothered to ask... Does anyone know why some people do: struct MYSTRUCT { int someint; char somechar; etc... }; vs. typedef struct { int someint; char somechar; etc... } MYSTRUCT; The 2nd version always seemed a bit odd to me, since in C++ everything else has the names at top. (If you define a function, the name goes at the top, same with classes, etc.) So, is there any advantage to the 2nd version? Maybe it has something to do w/ compatibility with plain C compilers? Thanks, roos

##### Share on other sites
The typedef version is an old trick C programmers used, it's completely unnecessary in C++, (so if you're using C++ you should use the first version).

Apparently in C if you simply said
struct A{int something;};

Everytime you wanted to create an object of A you had to explicitly say "struct A" to let the compiler know "A" was a struct type. i.e. I believe you would have had to say
struct A a1;
Using the typedef method you remove the need to say "struct A" so you can simply do:
A a1;
like you do in C++.

Oh, and as a small aside you should leave ALL_CAPS for macros and #defines

##### Share on other sites
Quote:
 Oh, and as a small aside you should leave ALL_CAPS for macros and #defines

I disagree with that. And apparently others do too... If you notice the structs declared in windows.h are all caps. Same with directx.

##### Share on other sites
Quote:
Quote:
 Oh, and as a small aside you should leave ALL_CAPS for macros and #defines

I disagree with that. And apparently others do too... If you notice the structs declared in windows.h are all caps. Same with directx.

This may sound weird, but that was a major factor in my decision to learn SDL/OpenGL over Win32/DirectX[smile]

##### Share on other sites
Quote:
 Original post by roosHi,I've always wondered about this but never bothered to ask... Does anyone know why some people do:struct MYSTRUCT{int someint;char somechar;etc...};vs.typedef struct{int someint;char somechar;etc...} MYSTRUCT;The 2nd version always seemed a bit odd to me, since in C++ everything else has the names at top. (If you define a function, the name goes at the top, same with classes, etc.)So, is there any advantage to the 2nd version? Maybe it has something to do w/ compatibility with plain C compilers?Thanks,roos

The first is a C++ class (struct) definition. The second is the same, but without the class name. You're creating a struct (class) without a name, and declaring it a new type. Many compilers will list the second type as an unnamed struct.

##### Share on other sites
Quote:
Original post by Simian Man
Quote:
Quote:
 Oh, and as a small aside you should leave ALL_CAPS for macros and #defines

I disagree with that. And apparently others do too... If you notice the structs declared in windows.h are all caps. Same with directx.

This may sound weird, but that was a major factor in my decision to learn SDL/OpenGL over Win32/DirectX[smile]

On a related note, ALLCAPS is the tool of the devil and lawyers. You know why they put the important stuff in allcaps? It's because they don't want you to read that stuff.

</conspiracytheorist>

In seriousness, most humans interpret words in large part based on the heights of their letters. With ALLCAPS, this is a constant, which slows comprehension and can induce strain. As such, I reserve ALLCAPS only for dirty things like macros.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632983
• Total Posts
3009708
• ### Who's Online (See full list)

There are no registered users currently online

×