I have seen many programs consisting of structures like the one below
typedef struct
{
int i;
char k;
} elem;
elem user;
Why is it needed so often? Any specific reason or applicable area?
I have seen many programs consisting of structures like the one below
typedef struct
{
int i;
char k;
} elem;
elem user;
Why is it needed so often? Any specific reason or applicable area?
Let's start with the basics and work our way up.
Here is an example of Structure definition:
Here the name
point
is optional.A Structure can be declared during its definition or after.
Declaring during definition
Declaring after definition
Now, carefully note the last case above; you need to write
struct point
to declare Structures of that type if you decide to create that type at a later point in your code.Enter
typedef
. If you intend to create new Structure ( Structure is a custom data-type) at a later time in your program using the same blueprint, usingtypedef
during its definition might be a good idea since you can save some typing moving forward.A word of caution while naming your custom type
Nothing prevents you from using _t suffix at the end of your custom type name but POSIX standard reserves the use of suffix _t to denote standard library type names.
I don't think forward declarations are even possible with typedef. Use of struct, enum, and union allow for forwarding declarations when dependencies (knows about) is bidirectional.
Style: Use of typedef in C++ makes quite a bit of sense. It can almost be necessary when dealing with templates that require multiple and/or variable parameters. The typedef helps keep the naming straight.
Not so in the C programming language. The use of typedef most often serves no purpose but to obfuscate the data structure usage. Since only { struct (6), enum (4), union (5) } number of keystrokes are used to declare a data type there is almost no use for the aliasing of the struct. Is that data type a union or a struct? Using the straightforward non-typdefed declaration lets you know right away what type it is.
Notice how Linux is written with strict avoidance of this aliasing nonsense typedef brings. The result is a minimalist and clean style.
From an old article by Dan Saks (http://www.ddj.com/cpp/184403396?pgno=3):
The linked article also has a discussion about how the C++ behavior of not requireing a
typedef
can cause subtle name hiding problems. To prevent these problems, it's a good idea totypedef
your classes and structs in C++, too, even though at first glance it appears to be unnecessary. In C++, with thetypedef
the name hiding become an error that the compiler tells you about rather than a hidden source of potential problems.the name you (optionally) give the struct is called the tag name and, as has been noted, is not a type in itself. To get to the type requires the struct prefix.
GTK+ aside, I'm not sure the tagname is used anything like as commonly as a typedef to the struct type, so in C++ that is recognised and you can omit the struct keyword and use the tagname as the type name too:
typedef will not provide a co-dependent set of data structures. This you cannot do with typdef:
Of course you can always add:
What exactly is the point of that?
One other good reason to always typedef enums and structs results from this problem:
Notice the typo in EnumDef in the struct (EnuumDef)? This compiles without error (or warning) and is (depending on the literal interpretation of the C Standard) correct. The problem is that I just created an new (empty) enumeration definition within my struct. I am not (as intended) using the previous definition EnumDef.
With a typdef similar kind of typos would have resulted in a compiler errors for using an unknown type:
I would advocate ALWAYS typedef'ing structs and enumerations.
Not only to save some typing (no pun intended ;)), but because it is safer.