Skip to main content
Engineering LibreTexts

5.6: Typedef - An Alias

  • Page ID
    29053
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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;
    

    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 use
    cash  income;
    cash  rent;
    cash  vacation;
    

    So, we can use typedef to create an alias. In the above example we create a new type of 'cash' - and then we can use this new type just as we would use int, or float, or double to define a variable. the variable income is actually a double, because we stated this using the typedef instruction.

    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.

    Adapted from: 
    "Typedef - an alias" by Kenneth Leroy Busbee, (Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2) is licensed under CC BY 4.0


    This page titled 5.6: Typedef - An Alias is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.