一起來學cdecl

來源:互聯網
上載者:User

 

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型的指標)。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.