cdecl 的一個非常強大的功能就是能夠分析C文法中非常複雜的定義。
第一部分:用cdecl來學學const 在C語言的功能。
運行cdecl,
(1)const char * i;
cdecl> explain const char * i
declare i as pointer to const char
翻譯: 定義i為指標,指向const類型的char。
(2) char *const i;
cdecl> explain char *const i
declare i as const pointer to char
翻譯: 定義i為const型的指標,指向char。
(3)那麼有沒有char const *i; 這樣的用法呢?那我們就試試吧,
cdecl> explain char const * i
syntax error
是否真的是文法錯呢?那就得看看《The C Programming Language》了, 在A.8.6.1 Pointer Declarator 只有上面兩種用法。於是用gcc試試,發現可以編譯過去,和const char *i是一樣,不知道是不是cdecl太嚴謹了呢,不過const *這種表示的確不好看。那const * char i; 呢?不過這就不用試了吧,*char 是個什麼東西呀。
注意:網上有篇《水滴石穿C語言程式設計之正確使用const》其中有錯誤,比如const (char *) pContent; 這種定義的方法在cdecl和gcc 都報錯。
第二部分: 用cdecl 學學函數指標。
(1) char (*a)()
cdecl> explain char (*a)()
declare a as pointer to function returning char
翻譯: 定義a是一個函數指標,指向的函數返回char型。
(2) char (*a[])() 是什麼?
cdecl> explain char (*a[])()
declare a as array of pointer to function returning char
翻譯: a 是一個函數指標數組,數群組成員指向的函數都是返回char型。
(3)char ((*a[])())() 又是什麼???????
cdecl> explain char ((*a[])())()
declare a as array of pointer to function returning function returning
char
翻譯: a是一個指標數組,數群組成員指向返回 "函數" 指標的函數,(雙引號中的函數的傳回值是char型)。
(4) char *((*a[])())() 和上面的區別就不大了
cdecl> explain char *((*a[])())()
declare a as array of pointer to function returning function returning pointer
to char
翻譯: a是一個指標數組,數群組成員指向返回 "函數" 指標的函數,(雙引號中的函數的傳回值是char型的指標)。