typedef
typedef
is a keyword in the C and C++ programming languages. It is used to give a data type a new name. The intent is to make it easier for programmers to comprehend source code.
Usage examples
Consider this code:
int coxes;
char jaffa;
...
coxes++;
...
if (jaffa == 'c')
...
Now consider this:
typedef int Apple;
typedef char Orange;
Apple coxes;
Orange jaffa;
...
coxes++;
...
if (jaffa == 'c')
...
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:
struct var {
int data1;
int data2;
char data3;
};
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:
struct var a; /*This piece of code is included in the main program where the structure
is to be used or in any program that is going to use structure var.
Here 'a' is a structure variable of user defined type "var".
Else the structure variable can also be created along with
the structure creation as below: */
struct var {
int data1;
int data2;
char data3;
}a;
Let's add the following line to the end of this example:
typedef struct var newtype;
Now, in order to create a variable of type var, the following code will suffice:
newtype a;
The same can be accomplished with the following code:
typedef struct var {
int data1;
int data2;
char data3;
} newtype;
Typedefs can also simplify creating pointers to self-referential structures. Consider this:
struct Node {
int data;
struct Node *nextptr;
};
Normally, one would need to prefix an asterisk to each variable to assign it a pointer:
struct Node *startptr, *endptr, *curptr, *prevptr, errptr, *refptr;
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.
typedef struct Node *NodePtr;
...
NodePtr startptr, endptr, curptr, prevptr, errptr, refptr;
Now it is guaranteed that all variables defined are of type Node *, even errptr.
Note that a struct
declaration in C++ also defines an implicit typedef
— that is, the data type can be referred to as var
(rather than struct var
) immediately, without any explicit use of the typedef
keyword. However, even in C++ it can be worthwhile to define typedefs for more complicated types. For example, a program to manipulate RGB images might find it useful to give the name ImagePointer
to the type "pointer to array[3] of int
":
typedef int (*ImagePointer)[3];
Usage concerns
Some people are opposed to the extensive use of typedefs. Most arguments center on the idea that typedefs simply hide the actual data type of a variable. For example, Greg Kroah-Hartman, a Linux 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[1].
Others argue that the use of typedefs can make code easier to maintain. K&R states that there are two reasons for using a typedef. First, it provides a means to make a program more portable. Instead of having to change a type everywhere it appears throughout the program's source files, only a single typedef statement needs to be changed. Second, a typedef can make a complex declaration easier to understand.
A third reason is that a typedef will allow a programmer to take advantage of the strong type checking of C/C++. Many times programmers will simply use a #define instead of a typedef but this loses the advantage of communicating your intention to the compiler.
Usage in C++
In C++ type names can be very complicated and typedef provides a mechanism to assign a simple name to the type. Consider:
std::vector<std::pair<std::string, int> > values;
for (std::vector<std::pair<std::string, int> >::const_iterator i = values.begin(); i != values.end(); ++i)
{
std::pair<std::string, int> const & t = *i;
// do something
}
and
typedef std::pair<std::string, int> value_t;
typedef std::vector<value_t> values_t;
values_t values;
for (values_t::const_iterator i = values.begin(); i != values.end(); ++i)
{
value_t const & t = *i;
// do something
}
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:
type PairOfInts = (Int, Int)
This example has defined a type synonym "PairOfInts" which means the same as a pair of Ints.
C# also contains a feature which is similar to the typedef of C. See this MSDN article for more
See also
References
- ↑ Kroah-Hartman, Greg (2002-07-01). "Proper Linux Kernel Coding Style". Linux Journal. http://www.linuxjournal.com/article/5780. Retrieved 2007-09-23. "Using a typedef only hides the real type of a variable."
If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...