?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
的typedef聲明提供了一種方式來聲明的標(biāo)識符作為一個類型別名,要使用的替換可能復(fù)雜的類型名稱。
該關(guān)鍵字typedef
在聲明中用于存儲類說明符的語法位置,但不影響存儲或關(guān)聯(lián):
typedef int int_t; // declares int_t to be an alias for the type inttypedef char char_t, *char_p, (*fp)(void); // declares char_t to be an alias for char // char_p to be an alias for char* // fp to be an alias for char(*)(void)
如果聲明使用typedef
存儲類說明符,則其中的每個聲明符都將標(biāo)識符定義為指定類型的別名。由于聲明中只允許使用一個存儲類說明符,因此typedef聲明不能為靜態(tài)或外部。
typedef聲明不會引入獨(dú)特的類型,它只會為現(xiàn)有類型建立同義詞,因此typedef名稱與它們所屬的類型兼容。Typedef名稱與普通標(biāo)識符(如枚舉器,變量和函數(shù))共享名稱空間。
VLA的typedef只能出現(xiàn)在塊范圍內(nèi)。每次控制流經(jīng)過typedef聲明時,都會評估數(shù)組的長度,而不是聲明數(shù)組本身:void copyt(int n){typedef int Bn; // B是VLA,其大小為n,現(xiàn)在評估n + = 1; B a; // a的大小是從之前的+ n = 1 int bn; // a和b的大小不同(int i = 1; i <n; i ++)ai-1 = bi; } | (自C99以來) |
---|
typedef名稱可能是不完整的類型,可以像往常一樣完成:
typedef int A[]; // A is int[]A a = {1, 2}, b = {3,4,5}; // type of a is int[2], type of b is int[3]
通常使用typedef聲明將名稱從標(biāo)記名稱空間注入到普通名稱空間中:
typedef struct tnode tnode; // tnode in ordinary name space // is an alias to tnode in tag name spacestruct tnode { int count; tnode *left, *right; // same as struct tnode *left, *right;}; // now tnode is also a complete typetnode s, *sp; // same as struct tnode s, *sp;
他們甚至可以避免使用標(biāo)簽名稱空間:
typedef struct { double hi, lo; } range;range z, *zp;
Typedef名稱通常也用于簡化復(fù)雜聲明的語法:
// array of 5 pointers to functions returning pointers to arrays of 3 intsint (*(*callbacks[5])(void))[3] // same with typedefstypedef int arr_t[3]; // arr_t is array of 3 inttypedef arr_t* (*fp)(void); // pointer to function returning arr_t*fp callbacks[5];
庫經(jīng)常將依賴于系統(tǒng)或依賴于配置的類型顯示為typedef名稱,以向用戶或其他庫組件提供一致的接口:
#if defined(_LP64)typedef int wchar_t;#elsetypedef long wchar_t;#endif
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
6.7.8類型定義(p:137-138)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
6.7.7類型定義(p:123-124)
C89 / C90標(biāo)準(zhǔn)(ISO / IEC 9899:1990):
3.5.6類型定義
typedef
.