Jump to content
  • Advertisement
Sign in to follow this  
kag1

Why Typedef a struct?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
<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>

Share this post


Link to post
Share on other sites
<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>

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 ;)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hmm... for some reason the words I put between angle brackets dissapeared!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!