Why Typedef a struct?

Started by
13 comments, last by MaulingMonkey 19 years ago
Hi, i'm just curious why some people use

typedef struct
{
float x, y, z;
} sVertex;
and some people use

struct sVertex
{
float x, y, z;
};
Is it just a preference thing? They are both the same correct?
Advertisement
in C you need the typedef to get the shorter name else you would need to write struct sVertext, in C++ you can consider them equvivalent.

HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
the first example is for strict C programming. you have to typedef the struct.
the second example is for C++ programming. you can typedef but you don't have to.

Beginner in Game Development?  Read here. And read here.

 

The second form provides namespace separation in C, enabling you to define variables like so:

struct vertex {float x, y, z;}; // define the type
struct vertex vertex; // construct a variable of that type

So the name vertex can describe both the structure's type and an instance. A similar thing can be done with enums.
Quote:Original post by dcosborn
The second form provides namespace separation in C, enabling you to define variables like so:

struct vertex {float x, y, z;}; // define the type
struct vertex vertex; // construct a variable of that type

So the name vertex can describe both the structure's type and an instance. A similar thing can be done with enums.

<offtopic>
If I ever saw someone who wrote code like that without a really good reason, I'd set them on fire [smile]
</offtopic>
<offtopic>
Quote:Original post by Evil Steve

If I ever saw someone who wrote code like that without a really good reason, I'd set them on fire [smile]


And I would follow up with a can of gasoline [smile]

</offtopic>

Raymond Jacobs, Owner - Ethereal Darkness Interactive
www.EDIGames.com - EDIGamesCompany - @EDIGames

<offtopic>
Quote:Original post by EDI
<offtopic>
Quote:Original post by Evil Steve

If I ever saw someone who wrote code like that without a really good reason, I'd set them on fire [smile]


And I would follow up with a can of gasoline [smile]

</offtopic>


Well crap. Remind me never to invite you two over to any coding parties or something :P.

(although usually I use the form namespace::type variable;, so I have code like: evoc::reporter & reporter;... the programming language I'm developing will have prefixed types, so that the above turns out into: @reporter & reporter). I feel that being able to have nonconflicting type and variable names is necessary for cleanly written generic programming - aka, a function that manipulates a generic string should be able to take an argument: string & string; instead of resorting to string & string_to_modify or string & String or other such silly naming).

</offtopic>
That's incorrect. Both in C and C++:
struct Name {
someType someContent;
};
is the definition of the structure

struct {
someTypeSomeContent;
} Name;
makes 'Name' an instance of an anonymous struct

You NEVER have to typedef a struct, as structs and typedef are completely different sentences. Typedef acts this way:
typedef ;
where is any data type, and is the name of the new data type we have just defined. newType can be seen as an alias for type.

So
typedef struct Name {someType someContent;} CustomType;
is equivalent to
struct Name {
someType someContent;
};
typedef struct Name CustomType;
Now, instead of:
struct Name variable1;
you can do:
CustomType variable1;

It's actually easy ;)
Hmm... for some reason the words I put between angle brackets dissapeared!
Quote:Original post by Anonymous Poster
Hmm... for some reason the words I put between angle brackets dissapeared!


that'd be that crazy HTML thing we keep hearing about..... [grin]
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement