本文译自 spiral rule,后附全文.
顺时针/螺旋式规则
顺时针/螺旋式规则是一种能让任何C程序员理解程序声明的方法。
如下3个步骤:
1.从要确定类型的元素开始,按顺时针方向把下面遇到的元素替换为相应的语句,例如:
[X] or []
==>Array X size of ... or Array undefined size of ..;
(type1,type2)
==>function passing type1 and type2 returning...
*
==>pointer(s) to ...
2.一直以这种方式替换遇到的元素直到所有元素都被替换。
3.总是先处理小括号中的内容。
栗子
#1:简单声明
+-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+
先问自己:str是什么?
“str 是
a.我们以‘str‘开始,按顺时针方向,遇到的第一个符号是‘[‘,表明这是一个数组,所以:
‘str‘是一个包含10个XX的数组;(XX代表目前还未决定的)
b.继续以顺时针方向移动,下一个遇到的是‘*‘,说明这是一个指针,所以:
‘str‘是一个包含10个XX指针的数组;
c.继续顺时针方向移动,现在遇到了行尾的‘;‘,所以继续移动并且遇到了类型‘char‘,所以:
‘str‘是一个包含10个char指针的数组;
#2:函数指针声明
+--------------------+
| +---+ |
| |+-+| |
| |^ || |
char *(*fp)( int, float *);
^ ^ ^ || |
| | +--+| |
| +-----+ |
+------------------------+
先自问:fp是什么?
"fp是一个"
a.顺时针方向移动,我们遇到的第一个元素是‘)‘,因此,fp在内括号里,所以继续移动(在圆括号范围里,Rule NO 3),
遇到的下一个符号是‘*‘,所以:
fp是一个指向XX的指针;
b.跳出括号,继续以顺时针方向移动,遇到‘(‘,因此,这是一个函数,所以:
fp是指向一个返回XX的函数(函数的参数是int和float指针)的指针;
c.继续顺时针移动,遇到‘*‘符号,所以:
fp是指向一个返回XX指针的函数的指针;
d.继续顺时针移动,遇到‘;‘但是我们目前还没有访问到所有的元素,继续移动遇到类型‘char‘,所以:
fp是指向一个返回char指针的函数指针;
#3:"Ultimate"
+-----------------------------+
| +---+ |
| +---+ |+-+| |
| ^ | |^ || |
void (*signal(int, void (*fp)(int)))(int);
^ ^ | ^ ^ || |
| +------+ | +--+| |
| +--------+ |
+----------------------------------+
提问:‘signal‘是什么?
注意,‘signal‘是在括号内,所以要优先处理;
a.顺时针方向移动,遇到‘(‘,所以:
‘signal‘是一个函数(传递的参数是int和一个?)
b.哈哈,我们可以使用同样的规则来处理‘fp‘,同样的问题:‘fp‘是什么?由于‘fp‘也在一个括号内,继续移动看到‘*‘,
所以:
‘fp‘是一个指针;
b1.继续顺时针移动,遇到‘(‘,所以:
‘fp‘是一个指向函数(函数参数是int,返回XX)的指针;
b2.继续移动到括号外,遇到‘void‘;所以:
‘fp‘是一个指向函数(函数参数是int,返回为空)的指针;
‘c.fp的解析结束了,我们继续看‘signal‘,到现在为止我们知道:
‘signal‘是一个函数(参数是int和一个指向函数(函数参数是int,返回为空)的指针);
d.我们依然在括号中,所以遇到的下一个符号是‘*‘,所以:
‘signal‘是一个函数(参数是int和一个指向函数(函数参数是int,返回为空)的指针)返回指向XX的指针;
e.现在我们解决了括号内的所有元素,继续顺时针移动,我们遇到了另一个‘(‘,所以:
‘signal‘是一个函数(参数是int和一个指向函数(函数参数是int,返回为空)的指针)返回指向函数(参数是int,返回XX的函数)的指针;
f.最后,继续移动处理剩下的那个符号‘void‘,所以,最终的定义是:
‘signal‘是一个函数(参数是int和一个指向函数(函数参数是int,返回为空)的指针)返回指向函数(参数是int,返回为空的函数)的指针;
这个规则对于const和volatile(http://en.wikipedia.org/wiki/Volatile_(computer_programming))同样适用。
例如:
const char *chptr;
a.‘chptr‘是什么?
‘chptr‘是指向一个char常量的指针;
这个栗子:
char * const chptr;
a.chptr是什么?
‘chptr‘是指向char类型的常量指针;
最后的栗子:
volatile char * const chptr;
a.‘chptr‘是什么?
chptr是个‘char volatile‘的常量指针
[原文]
[This was posted to comp.lang.c by its author, David Anderson, on 1994-05-06.]
The ``Clockwise/Spiral Rule‘‘
By David Anderson
There is a technique known as the ``Clockwise/Spiral Rule‘‘ which enables any C programmer to parse in their head any C declaration!
There are three simple steps to follow:
Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements:
[X] or []
=> Array X size of... or Array undefined size of...
(type1, type2)
=> function passing type1 and type2 returning...
*
=> pointer(s) to...
Keep doing this in a spiral/clockwise direction until all tokens have been covered.
Always resolve anything in parenthesis first!
Example #1: Simple declaration
+-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+
Question we ask ourselves: What is str?
``str is an...
We move in a spiral clockwise direction starting with `str‘ and the first character we see is a `[‘ so, that means we have an array, so...
``str is an array 10 of...
Continue in a spiral clockwise direction, and the next thing we encounter is the `*‘ so, that means we have pointers, so...
``str is an array 10 of pointers to...
Continue in a spiral direction and we see the end of the line (the `;‘), so keep going and we get to the type `char‘, so...
``str is an array 10 of pointers to char‘‘
We have now ``visited‘‘ every token; therefore we are done!
Example #2: Pointer to Function declaration
+--------------------+
| +---+ |
| |+-+| |
| |^ || |
char *(*fp)( int, float *);
^ ^ ^ || |
| | +--+| |
| +-----+ |
+------------------------+
Question we ask ourselves: What is fp?
``fp is a...
Moving in a spiral clockwise direction, the first thing we see is a `)‘; therefore, fp is inside parenthesis, so we continue the spiral inside the parenthesis and the next character seen is the `*‘, so...
``fp is a pointer to...
We are now out of the parenthesis and continuing in a spiral clockwise direction, we see the `(‘; therefore, we have a function, so...
``fp is a pointer to a function passing an int and a pointer to float returning...
Continuing in a spiral fashion, we then see the `*‘ character, so...
``fp is a pointer to a function passing an int and a pointer to float returning a pointer to...
Continuing in a spiral fashion we see the `;‘, but we haven‘t visited all tokens, so we continue and finally get to the type `char‘, so...
``fp is a pointer to a function passing an int and a pointer to float returning a pointer to a char‘‘
Example #3: The ``Ultimate‘‘
+-----------------------------+
| +---+ |
| +---+ |+-+| |
| ^ | |^ || |
void (*signal(int, void (*fp)(int)))(int);
^ ^ | ^ ^ || |
| +------+ | +--+| |
| +--------+ |
+----------------------------------+
Question we ask ourselves: What is `signal‘?
Notice that signal is inside parenthesis, so we must resolve this first!
Moving in a clockwise direction we see `(‘ so we have...
``signal is a function passing an int and a...
Hmmm, we can use this same rule on `fp‘, so... What is fp? fp is also inside parenthesis so continuing we see an `*‘, so...
fp is a pointer to...
Continue in a spiral clockwise direction and we get to `(‘, so...
``fp is a pointer to a function passing int returning...‘‘
Now we continue out of the function parenthesis and we see void, so...
``fp is a pointer to a function passing int returning nothing (void)‘‘
We have finished with fp so let‘s catch up with `signal‘, we now have...
``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning...
We are still inside parenthesis so the next character seen is a `*‘, so...
``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to...
We have now resolved the items within parenthesis, so continuing clockwise, we then see another `(‘, so...
``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to a function passing an int returning...
Finally we continue and the only thing left is the word `void‘, so the final complete definition for signal is:
``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning a pointer to a function passing an int returning nothing (void)‘‘
The same rule is applied for const and volatile. For Example:
const char *chptr;
Now, what is chptr??
``chptr is a pointer to a char constant‘‘
How about this one:
char * const chptr;
Now, what is chptr??
``chptr is a constant pointer to char‘‘
Finally:
volatile char * const chptr;
Now, what is chptr??
``chptr is a constant pointer to a char volatile.‘‘
Practice this rule with the examples found in K&R II on page 122.
Copyright © 1993,1994 David Anderson
This article may be freely distributed as long as the author‘s name and this notice are retained.