- Typedef
typedef
is a keyword in the C andC++ programming language s. It is used to give adata type a new name. The intent is to make it easier for programmers to comprehendsource code .Usage examples
Consider this code:
Now consider this:
Both sections of code do the same thing. The use of
typedef
in the second example makes it easier to comprehend what is going on, namely, that one variable contains information about apples while the other contains information about oranges.One more example:
Here a user-defined data type "var" has been defined. To create a variable of the type "var", in C, the following code is required:
Let's add the following line to the end of this example:
Now, in order to create a variable of type "var", the following code will suffice:
The same can be accomplished with the following code:
Typedefs can also simplify creating pointers to
self-referential structures . Consider this:Normally, one would need to prefix an asterisk to each variable to assign it a pointer:
A programmer would assume that errptr was indeed a Node *, but a typographical error means that errptr is a Node. This can lead to subtle syntax errors.
It would be better to define a Node * type. The following code does so.
Now it is guaranteed that all variables defined are of type Node *, even errptr.
Note that a
struct
declaration in C++ also defines an implicittypedef
— that is, the data type can be referred to asvar
(rather thanstruct var
) immediately, without any explicit use of thetypedef
keyword. However, even in C++ it can be worthwhile to define typedefs for more complicated types. For example, a program to manipulateRGB images might find it useful to give the nameImagePointer
to the type "pointer to array [3] ofint
":Criticisms
Some people are opposed to the extensive use of typedefs. Most arguments center around the idea that typedefs simply hide the actual data type of a variable. For example,
Greg Kroah-Hartman , aLinux kernel hacker and documenter, discourages their use for anything except function prototype declarations. He argues that this practice not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures thinking them to be simple types [cite web |url=http://www.linuxjournal.com/article/5780 |title=Proper Linux Kernel Coding Style |author=Kroah-Hartman, Greg |authorlink=Greg Kroah-Hartman |date=2002-07-01 |accessdate=2007-09-23 |work=Linux Journal |quote=Using a typedef only hides the real type of a variable.] .Other languages
In many statically-typed functional languages, like Haskell, Miranda,
OCaml , etc., you can define "type synonyms", which are the same as typedefs in C. An example in Haskell:
We've just defined a type synonym "pairOfInts" which means the same as a pair of Int's.type pairOfInts = (Int, Int)ee also
*
Abstract data type
*C syntax References
External links
*http://www.cprogramming.com/tutorial/typedef.html - detailed discussion of typedef at "CProgramming.com"
Wikimedia Foundation. 2010.