# Why Typedef a struct?

## Recommended Posts

kag1    146
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 on other sites
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.

##### Share on other sites
Alpha_ProgDes    6935
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 on other sites
dcosborn    674
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 on other sites
Evil Steve    2017
Quote:
 Original post by dcosbornThe second form provides namespace separation in C, enabling you to define variables like so:struct vertex {float x, y, z;}; // define the typestruct vertex vertex; // construct a variable of that typeSo 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 on other sites
EDI    3679
<offtopic>
Quote:
 Original post by Evil SteveIf 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 on other sites
MaulingMonkey    1730
<offtopic>
Quote:
Original post by EDI
<offtopic>
Quote:
 Original post by Evil SteveIf 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 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;
struct Name variable1;
you can do:
CustomType variable1;

It's actually easy ;)

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

##### Share on other sites
ChaosEngine    5185
Quote:
 Original post by Anonymous PosterHmm... for some reason the words I put between angle brackets dissapeared!

that'd be that crazy HTML thing we keep hearing about..... [grin]

##### Share on other sites
MaulingMonkey    1730
Quote:
Original post by ChaosEngine
Quote:
 Original post by Anonymous PosterHmm... for some reason the words I put between angle brackets dissapeared!

that'd be that crazy HTML thing we keep hearing about..... [grin]

Which although registered users can do, Anonymous Posters can not. The automatic HTML detection stuff quite liberally strips away the angle brackets of the Anonymous Posters before even deciding if it's HTML or if it needs to be escaped into &lt; and &gt; escapes (note: I had to escape the & sign to correctly display those without them getting converted... :P)

##### Share on other sites
dcosborn    674
One thing that my previously posted code might be useful for is something like a monolithic error system in C. Its seems rather clean to me to write a simple error reporting mechanism like so:

enum err
{
err_unknown = -1,
err_none,
err_file,
err_file_eof
};
enum err err;

This works since error is monolithic be nature - you're never going to need to create another instance of the err enum since err is globally accessible. It also functions similar to exceptions, enabling the error code to be checked without having to corrupt return values. It also propagates automatically until someone checks for it and handles it. The only thing is doesn't deal with is automatic destruction of objects, which isn't really done in the C paradigm.

Notice the fake heirarchy of error values using the underscore. err_file_eof is a subset of err_file. With some interesting bitmasking, the heirarchy could be more correctly modelled.

##### Share on other sites
Zahlman    1682
Quote:
 Original post by dcosbornOne thing that my previously posted code might be useful for is something like a monolithic error system in C.

Amazingly enough, you're not the first to think of it. [grin]

##### Share on other sites
dcosborn    674
[smile] That's another thing too. I like to model my interfaces on commonplace ones because:
1. They're tried and tested, and often make sense as a result.
2. They're standardized and used all over the place, so people understand them.

##### Share on other sites
MaulingMonkey    1730
Quote:
 Original post by Anonymous PosterThat's incorrect. Both in C and C++:struct Name { someType someContent;};is the definition of the structure

Correct, but you'd allways have to declare it using the struct keyword. Aka:

struct Name my_variable;

Adding the typedef allows one to simply type:

Name my_variable;

(IIRC anyways)