struct fruit mandarin; / uses structure tag "fruit" /
fruit tangerine; / uses structure type "fruit" /
Statement 2 declares a structure tag "veg" and a variable veg. Only the structure tag can be used in
further declarations, like this:
struct veg potato;
It would be an error to attempt a declaration of veg cabbage. That would be like writing:
int i;
ij;
Handy Heuristic
Tips for Working with Typedefs
Don't bother with typedefs for structs.
All they do is save you writing the word "struct", which is a clue that you probably
shouldn't be hiding anyway.
Use typedefs for:
- types that combine arrays, structs, pointers, or functions.
- portable types. When you need a type that's at least (say) 20-bits, make it a typedef.
Then when you port the code to different platforms, select the right type, short,
int, long, making the change in just the typedef, rather than in every
declaration.
- casts. A typedef can provide a simple name for a complicated type cast. E.g.
•
- typedef int (*ptr_to_int_fun)(void);
- char * p;
= (ptr_to_int_fun) p;
Always use a tag in a structure definition, even if it's not needed. It will be later.
A pretty good principle in computer science, when you have two different things, is to use two
different names to refer to them. It reduces the opportunities for confusion (always a good policy in
software). If you're stuck for a name for a structure tag, just give it a name that ends in "_tag". This