很多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 於 杭州