幫你解析複雜的C語言聲明

來源:互聯網
上載者:User

      很多C/C++的初學者,常常被一些複雜的聲明語句弄得暈頭轉向,滿頭霧水。比如說:char *(*(*a[])())(),這個聲明是什麼意思呢?

     不用抓耳撓腮,在linux環境下,有一個工具能幫我們解析,它就是cdecl 。它能在C/C++聲明和英語間轉換。讓我們先看看man cdecl裡的描述吧:

DESCRIPTION
       Cdecl  (and c++decl) is a program for encoding and decoding C (or C++) type declarations.  The C language is based on the (draft proposed) X3J11 ANSI Standard; optionally, the C language may be based on the pre- ANSI  definition  defined  by  Kernighan  &  Ritchie’s The C Programming Language book, or the C language  defined by the Ritchie PDP-11 C compiler.  The C++ language is based on Bjarne Stroustrup’s The C++  Pro-gramming Language, plus the version 2.0 additions to the language.

     很容易用:鍵入cdecl命令,進入互動介面:

# cdecl
Type `help' or `?' for help
cdecl>

這時鍵入命令:

explain char *(*(*a[])())()

看看輸出什麼了:

declare a as array of pointer to function returning pointer to function returning pointer to char
cdecl>

反過來,我們把輸出的英文當命令,再執行一次看看:

cdecl> declare a as array of pointer to function returning pointer to function returning pointer to char
char *(*(*a[])())()
cdecl>

     好用吧,如果有不明白,就help一下。

2010-12-28 於 杭州

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.