Saturday, September 5, 2009

typedef

Sometimes there is a need to have types that cannot operate together in the program. For instance, money and weight should not be added even though both can be saved into variables of the type double. In these cases the situation can be made clearer by giving an existing type a new name with the typedef definition. For example:

typedef doube Currency;

typedef does not create a new type. For instance the types double and Currency would be interchangeable.

Type definitions make the code easier to read. They can also be used to writing more portable code. The type int is on some machines only 16 bits long, for instance. If the program when moved to this machine needed to operate with large sums, only the typedefinition needs to be changed. All the gazillion points in the code using the alias would remain intact.

Reference: cprogramming, wikipedia, mmbase

Previous post

Next post


No comments:

Post a Comment