
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C - <b>typedef derived data type specifier</b>
[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
typedef Derived Data Type Specifier
The typedef keyword is used to invent a synonym for a type. This is
useful for two reasons: to give a type a name that is more meaningful
than one of C's basic types, and to give a name to a complex type
declaration (so you need not keep reading and writing that complex
type). The format of a typedef is:
typedef type declarator [[, declarator] ... ] ;
Notes: Many people don't use typedef--they use #define instead,
since it can achieve the same result. However, there are
many type declarations in which #define cannot be used in
place of typedef. The point to remember is that typedef
is part of the C language and it is processed by the
compiler, which knows about types--typedef was
specifically designed to give synonyms to types.
#define, on the other hand, is part of the preprocessor,
which knows nothing about C types--it merely provides a
string substitution facility, and should be restricted to
naming macros.
-------------------------------- Example ---------------------------------
typedef register fast;
typedef char *string;
typedef struct {double real, imag;} complex;
typedef struct link *FNODE();
fast total;
string name, state;
complex value;
FNODE getnode;
/* In this last example, it is not immediately obvious that
getnode is a function, since that property is hidden in
the typedef, which is typically placed in a header.
Therefore, the synonym should probably be defined as: */
typedef struct link *NODE; NODE getnode();
See Also: Types #define
Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson