typedef
根据现有类型定义新类型。它的语法反映了变量声明的语法。
/* Byte can be used wherever `unsigned char` is needed */
typedef unsigned char Byte;
/* Integer is the type used to declare an array consisting of a single int */
typedef int Integer[1];
/* NodeRef is a type used for pointers to a structure type with the tag "node" */
typedef struct node *NodeRef;
/* SigHandler is the function pointer type that gets passed to the signal function. */
typedef void (*SigHandler)(int);
虽然技术上不是存储类,但编译器会将其视为一个存储类,因为如果使用 typedef
关键字,则不允许使用其他任何存储类。
typedef
s 很重要,不应该用 #define
宏代替。
typedef int newType;
newType *ptr; // ptr is pointer to variable of type 'newType' aka int
然而,
#define int newType
newType *ptr; // Even though macros are exact replacements to words, this doesn't result to a pointer to variable of type 'newType' aka int