A typedef
declaration has the same syntax as a variable or function declaration, but it contains the word typedef
. The presence of typedef
causes the declaration to declare a type instead of a variable or function.
int T; // T has type int
typedef int T; // T is an alias for int
int A[100]; // A has type "array of 100 ints"
typedef int A[100]; // A is an alias for the type "array of 100 ints"
Once a type alias has been defined, it can be used interchangeably with the original name of the type.
typedef int A[100];
// S is a struct containing an array of 100 ints
struct S {
A data;
};
typedef
never creates a distinct type. It only gives another way of referring to an existing type.
struct S {
int f(int);
};
typedef int I;
// ok: defines int S::f(int)
I S::f(I x) { return x; }