Die allgemeine Typdefinition
typedef <type_definition> <type_bezeichner>
ist die konsequente Weiterentwicklung zu frei definierbaren Typen.
Das nachfolgende Programmbeispiel illustriert die Definition der drei neuen Typen Boolean, Text und Point3D.
// general type definitions
main()
{
// new types
typedef short int Boolean;
typedef char Text[100];
typedef struct
{
double x,y,z;
} Point3D;
// new variables
Boolean a,b;
Text eintrag;
Point3D pts[10], p = {1, 2, 3.45};
...
}
gesamtes Programm
// general type definitions
#include <iostream.h>
#include <string.h>
main()
{
// new types
typedef short int Boolean;
typedef char Text[100];
typedef struct
{
double x,y,z;
} Point3D;
// new variables
Boolean a,b;
Text eintrag;
Point3D pts[10], p = {1,2,3.45};
// init
strcpy(eintrag,"A beautiful code");
pts[0] = p;
a = (p.x == pts[0].x); // boolean
b = !a; // boolean
// output
cout << endl;
cout << eintrag << endl;
cout << "p = " << p.x << " , " << p.y << " , " << p.z << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
Interessanterweise ist eine Variable vom Typ Text nunmehr stets eine Zeichenkettenvariable der (max.) Länge 100. Man beachte auch die Initialisierung der Variablen p. Damit kann sogar eine Konstante vom Typ Point3d deklariert und initialisiert werden.