This page is optimized for mobile devices, if you would prefer the desktop version just click here

9.2 Typedef - an alias

An explanation of typedef being used to create an alias data type.

General discussion

The typedef statement allows the programmer to create an alias, or synonym, for an existing data type. This can be useful in documenting a program. The C++ programming language syntax is:

typedef<the real data type><the alias identifier name>;

Let's say a programmer is using a double data type to store the amount of money that is being used for various purposes in a program. He might define the variables as follows:

Regular definition of variables

double income; double rent;double vacation;

Got questions? Get instant answers now!

However, he might use the typedef statement and define the variables as follows:

Using typedef when defining variables

typedef double cash; the typedef must be defined before its usecash income; cash rent;cash vacation;

Got questions? Get instant answers now!

The typedef statement is not used very often by beginning programmers. It usually creates more confusion than needed, thus stick to using the normal data types at first.

Definitions

typedef
Allows the programmer to create an alias, or synonym, for an existing data type.
<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Programming fundamentals - a modular structured approach using c++. OpenStax CNX. Jan 10, 2013 Download for free at http://cnx.org/content/col10621/1.22
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.