Basically a typedef
has exactly the same syntax as an object declaration except that it is prefixed with typedef
. Doing that changes the meaning of the declaration so that the new identifier declares an alias for the type that the object that would have been declared, had it been a normal declaration, would have had.
A typedef
is scoped exactly as the object declaration would have been, so it can be file scoped or local to a block or (in C++) to a namespace or class.
e.g.
Declares an int
:
int a;
Declares a type that is an alias for int
:
typedef int a_type;
Declares a pointer to a char
:
char *p;
Declares an alias for a char *
:
typedef char *pChar;
Declares a function pointer:
int (*pFn)(int);
Declares an alias for the type that is 'pointer to a function taking int
and returning int
':
typedef int (*pFunc)(int);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…