Tuesday, July 1, 2008

C++ quick tutorial: typedef

Suppose you need to define lots of variables as unsigned short int, for example:

unsigned int a[3];

It can be useful in these situations to use typedef, which enables you to create aliases to any of the usual C++ datatypes. For example:

typdef unsigned int UnsignedInt;
...
UnsignedInt a[3];

In this example we have created an alias called UnsignedInt which we can then use to declare variables of type unsigned int.

Another example, this time using char:

typedef char[60] field;
...
field name;

Here we have created an alias called field which can be used to declare variables of type char[60].

No comments: