Typedef

Introduction

A typedef lets us define a synonym for a type.
Typedefs allow the programmer to create an alias for a data type, and use the aliased name instead of the actual type name.

The syntax is :
typedef existing-type-description alias-name

Example: Synonym for primitive types

     typedef double wages;       //  wages is a synonym for double
     typedef int exam_score;     //  exam_score is a synonym for int
     typedef wages salary;       //  indirect synonym for double
 
//A typedef name can be used as a type specifier:
 
     wages hourly, weekly;     // double hourly, weekly;
     exam_score test_result;   // int test_result;

Example
The following code produces ant int* which is x and an int which is y. The * binds to the right, not the left.

int* x,y;

Better:

typedef int* intPtr;
intPtr x, y;

Example: Synonym for structs
A structure is a way to collect a group of variables into a structure.

struct Structure1
{
    char c;
    int i;
};
 
int main() 
{
  struct Structure1 s1, s2;
  s1.c = 'a'; // Select an element using a '.'
  s1.i = 1;
  s1.f = 3.14;
  s1.d = 0.00093;
  s2.c = 'a';
  s2.i = 1;
  s2.f = 3.14;
}

In C you can't just say Structure1 when you are defining variables. You must say struct Structure.

Better:

typedef struct {
  char c;
  int i;
  float f;
  double d;
} Structure2;
 
int main() 
{
  Structure2 s1, s2;
  s1.c = 'a';
  s1.i = 1;
  s1.f = 3.14;
  s1.d = 0.00093;
  s2.c = 'a';
  s2.i = 1;
  s2.f = 3.14;
  s2.d = 0.00093;
}

Example: Synonym for functions
The typedef keyword can also be used to define an alias for a function. In this case, you must specify the return type of the function and its type(s) of argument(s), if any. Also the definition must specify that it is a function pointer.

#include <iostream>
#include <string>
using namespace std;
 
typedef short SmallNumber;
typedef unsigned int Positive;
typedef double* PDouble;
typedef string FiveStrings[5];
typedef double (*Addition)(double value1, double value2);
 
double add(double x, double y)
{
    double result = x + y;
    return result;
}
 
int main()
{
    SmallNumber temperature = -248;
    Positive height = 1048;
    PDouble distance = new double(390.82);
    FiveStrings countries = { "Ghana", "Angola", "Togo",
                              "Tunisia", "Cote d'Ivoire" };
    Addition plus;
 
    cout << "Temperature: " << temperature << endl;
    cout << "Height:      " << height << endl;
    cout << "Distance:    " << *distance << endl;
    cout << "Countries:\n";
    for(int i = 0; i < sizeof(countries) / sizeof(string); i++)
        cout << '\t' << countries[i] << endl;
 
    plus = add;
    cout << "3855.06 + 74.88 = " << plus(3855.06, 74.88) << endl;
    return 0;
}

Example
fp3 is a pointer to a function that takes no arguments and returns a pointer to an array of 10 pointers to functions that take no arguments and return doubles!

typedef double (*(*(*fp3)())[10])();
 
fp3 a;

Synonym for an Array type

Example

typedef int Vector[20]; //20 integers
typedef int Vector[20]; //20 integers

Synonym for Enumerate Types

Example

typedef enum {RED, BLUE, GREEN} Color;
 
Color a, b;
a = RED;
 
+ Templates
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.