//  File:  Typedef.cpp

#include <iostream.h>

/*==================================================================
    Syntax:   typedef <type declaration> synonym;

    In contrast to the class, struct, union, and enum declarations, 
    typedef declarations DO NOT introduce new types -- instead they 
    introduce new names for existing types.

===================================================================*/

// typedef may be used to declare a simple type

typedef int boolean;

boolean Equal (const int nVal1, const int nVal2)
{  return (boolean) (nVal1 == nVal2);  }


// typedef may be used to declare structured types

typedef struct myTag
{
    int     i;
    double  d;
    char    c;
} MyStruct;

void Doit (MyStruct ms)
{
    MyStruct theStruct = ms;
}

// typedef may be used to declare array types

typedef int List [100];

void Sort (List list)
{
    for (int i = 0; i < 99; i++)
        for (int j = i+1; j < 100; j++)
            if (list [i] > list [j])
            {
                int nTmp = list [i];
                list [i] = list [j];
                list [j] = nTmp;
            }
}

// typedef frequently is used to improve clarity

struct Node;        // this "forward declaration" is necessary
typedef Node *NodePtr;

struct Node
{
    double  dData;
    NodePtr pNext;
};

// typedef may be used to work with function pointers

typedef Node (*FcnPtr) (const NodePtr ptr);

// FcnPtr specifies a pointer to a function that takes
// a single const pointer to a Node as an argument,
// and returns a Node

Node MakeNode (const NodePtr ptr)
{
    return *ptr;
}

void TryFcnPtr (void)
{
    FcnPtr fcnPtr = MakeNode;
    NodePtr pNode = new Node;
    Node node = (*fcnPtr) (pNode);
}


int main (void)
{
    cout << endl << "Demonstrate TYPEDEF" << endl << endl;

    return 0;
}

