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 關鍵字,則不允許使用其他任何儲存類。

typedefs 很重要,不應該用 #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