帮你解析复杂的C语言声明

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

你可能感兴趣的:(c,linux,function,语言,encoding,returning)