why do this:
typedef struct tagMyStruct
{
int x, y;
} myStruct;
When it is much easier to do this:
struct myStruct
{
int x, y;
};
I see no advantages to the typedef version. So why is it used by lots of people?
why do this:
typedef struct tagMyStruct
{
int x, y;
} myStruct;
When it is much easier to do this:
struct myStruct
{
int x, y;
};
I see no advantages to the typedef version. So why is it used by lots of people?
because some people code in C not C++ and they want to be able to refer to their structs without typing 'struct' each time.
typedef struct == bad
struct == good
The only thing typedef does in this situation is hide the fact that the programmer is using a struct. If you don't know what datatype the variable is in huge code that just makes reading and maintaining the code more difficult.
Translation: It's bad style for lazy people and should be avoided in anything but tiny programs.
>I see no advantages to the typedef version. So why is it used by lots of people?
That's because there are none.
-Prelude
My best code is written with the delete key.
*shrug*It is used purely for the sake of convenience. Putting 'struct' in front of every declaration of some kind of variable doesn't really tell you much about it. In fact, I'd say that having to state 'struct' each time would more likely confuse things, since it's the only case where a storage specifier is more than one word long.Code:int main (void) { struct node * list; // } vs... int main (void) { node * list; // }
If I could rewrite C, changing the way structs and unions (and maybe enums) are declared would be on my potential to-do list.
Callou collei we'll code the way
Of prime numbers and pings!
And if you use all uppercase letters for your struct names like the FILE structure for example, there is no confusion.
I don't see how it's lazy, if you wrote struct in front of your declarations you still would have to see the definitions to see what variables it contained.Translation: It's bad style for lazy people and should be avoided in anything but tiny programs.
Last edited by Edge; 01-26-2002 at 05:50 AM.
I usually use uppercase letters for names for constants. Names of structures I usually postfix (if that's correct English) by _s.
typedef struct node node_s;
And also for enums the postfix _e and there are some more. It's not a matter of lazyness, but a matter of making code more readable.
Documentation of code usually includes a document explaining the code conventions used and a document describing the detailed design. So when a new engineer gets into the project he/she doesn't need to figure out in the code what certain types mean.
In huge code especially a definition of clear code conventions is very important. If the conventions are good and the engineers do use the conventions in their code, then working with huge amounts of code can be quite easy.
So I don't mind people using typedefs as long as they're clear and well described.
>The only thing typedef does in this situation is hide the fact that >the programmer is using a struct.
It creates an Aliasname for your new datatype. It saves you a totally unneccessary information of 'struct' before each variable declaration. Everyone knows it's some kind of user defined variable.
>If you don't know what datatype the variable is in huge code >that just makes reading and maintaining the code more difficult.
Shouldn't that be clear by the type name ?
>Translation:
>It's bad style for lazy people and should be avoided
>in anything but tiny programs.
I don't think so. It saves typing and reading later.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
i with nv
ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.