现在的位置: 首页 > 综合 > 正文

顺时针螺旋法则(clockwise/spiral rule)教你认变量类型

2013年02月22日 ⁄ 综合 ⁄ 共 7335字 ⁄ 字号 评论关闭

The ``Clockwise/Spiral Rule'' 
“顺时针螺旋法则” 
  
By David Anderson 
作者: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! 
这个技巧称为“顺时针螺旋法则”。C程序员们用了它,就可以看懂任何C语言变量类型声明。 
  
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...  
         -> ...类型的长度为X的数组, 或者 ...类型的长度未知的数组 
     (type1, type2) 
         => function passing type1 and type2 returning...  
         -> 传入type1和type2类型,返回...类型的函数。 
     * 
         => 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 
例1: 简单声明 
  
                      +-------+ 
                      | +-+   | 
                      | ^ |   | 
                 char *str[10]; 
                  ^   ^   |   | 
                  |   +---+   | 
                  +-----------+ 
  
Question we ask ourselves: What is str? 
问题:str是什么类型? 
  
     ``str is an...  
     “str的类型是... 
  
     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”开始,遇到的第一个符号是 “[”。所以,它是个数组。 所以…… 
  
         ``str is an array 10 of...  
          “str是一个容纳……类型的长度为10的数组 
  
     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...  
         “str是一个容纳指向……的指针的长度为10的数组 
  
     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... 
     继续沿着顺治真螺旋移动。遇到了行末(其实就是分号“;”),不管它,继续移动,我们遇到了“char”。所以…… 
  
         ``str is an array 10 of pointers to char''  
         “str是一个容纳指向char类型的指针的长度为10的数组。” 
  
     We have now ``visited'' every token; therefore we are done!  
     我们已经“遍历”了每个符号。所以,完成了! 
  
Example #2: Pointer to Function declaration 
例2:指向函数的指针的声明 
  
                      +--------------------+ 
                      | +---+              | 
                      | |+-+|              | 
                      | |^ ||              | 
                 char *(*fp)( int, float *); 
                  ^   ^ ^  ||              | 
                  |   | +--+|              | 
                  |   +-----+              | 
                  +------------------------+ 
  
Question we ask ourselves: What is fp? 
问题:fp是什么类型? 
  
     ``fp is a...  
     “fp是一个…… 
  
     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被放在一个括号内。我们必须先留在括号内,继续沿着螺旋移动。下一个遇到的符号是“*”。所以…… 
  
         ``fp is a pointer to...  
         “fp是一个指向……的指针 
  
     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...  
         “fp是一个指向一个参数为一个int和一个指向float型的指针,返回值是……型的函数的指针。 
  
     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...  
         “fp是一个指向一个参数为一个int和一个指向float型的指针,返回值是一个指向……的指针的函数的指针。 
  
     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... 
     继续绕圈,遇到“;”,但是符号还没遍历完。继续绕圈,最后遇到char。所以…… 
  
         ``fp is a pointer to a function passing an int and a pointer to float returning a pointer to a char''  
         “fp是一个指向一餐参数为一个int和一个指向float型的指针,返回值是一个指向char的指针的函数的指针。” 
  
Example #3: The ``Ultimate'' 
例3:终极难题 
  
                       +-----------------------------+ 
                       |                  +---+      | 
                       |  +---+           |+-+|      | 
                       |  ^   |           |^ ||      | 
                 void (*signal(int, void (*fp)(int)))(int); 
                  ^    ^      |      ^    ^  ||      | 
                  |    +------+      |    +--+|      | 
                  |                  +--------+      | 
                  +----------------------------------+ 
  
Question we ask ourselves: What is `signal'? 
问题:signal是什么类型? 
  
Notice that signal is inside parenthesis, so we must resolve this first! 
首先,必须意识到signal在括号内。所以必须先解决括号内的类型。 
  
     Moving in a clockwise direction we see `(' so we have... 
     沿着顺时针移动,遇到“(”,所以…… 
  
         ``signal is a function passing an int and a...  
         “signal是一个参数为一个int和一个……型变量,返回值是……的函数。 
  
     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的类型。所以……fp是什么类型呢?fp也在括号内。所以,继续绕圈,我们遇见“*”。所以…… 
  
         fp is a pointer to...  
         fp是一个指向……的指针 
  
     Continue in a spiral clockwise direction and we get to `(', so... 
     继续绕圈,遇到“(”。所以…… 
  
         ``fp is a pointer to a function passing int returning...''  
         “fp是一个指向一个参数为int,返回值是……的函数的指针。” 
  
     Now we continue out of the function parenthesis and we see void, so... 
     离开函数参数的括号,我们遇见void。所以…… 
  
         ``fp is a pointer to a function passing int returning nothing (void)''  
         “fp是一个指向参数为int,什么也不返回(返回void)的函数的指针。” 
  
     We have finished with fp so let's catch up with `signal', we now have... 
     fp部分结束了。我们回到signal的类型。目前是…… 
  
         ``signal is a function passing an int and a pointer to a function passing an int returning nothing (void) returning...  
         “signal是一个参数为一个int和一个指向一个参数为int,什么也不返回的函数的指针,返回值为……的函数。” 
  
     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...  
         “signal是一个参数位一个int和一个指向一个参数为int,什么也不返回的函数的指针,返回值为指向……的指针的函数。” 
  
     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...  
         “signal是一个参数位一个int和一个指向一个参数为int,什么也不返回的函数的指针,返回值为指向一个参数为int,返回值为……的函数的指针的函数。” 
  
     Finally we continue and the only thing left is the word `void', so the final complete definition for signal is: 
     最后,继续绕圈。最后一个符号就是最左边的“void”。所以,signal的最终定义就是: 
  
         ``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)''  
         “signal是一个参数位一个int和一个指向一个参数为int,什么也不返回的函数的指针,返回值为指向一个参数为int,什么也不返回的函数的指针的函数。” 
  
The same rule is applied for const and volatile. For Example: 
同样的规则,const和volatile关键字也有用。比如: 
  
     const char *chptr; 
  
     Now, what is chptr?? 
     那么,chptr是什么类型?? 
  
         ``chptr is a pointer to a char constant''  
         “chptr是一个指向char型常量的指针变量。” 
  
How about this one: 
下面这个呢: 
  
     char * const chptr; 
  
     Now, what is chptr?? 
     现在,chptr又是什么?? 
  
         ``chptr is a constant pointer to char''  
         “chptr是一个指向char型变量的指针常量。” 
  
Finally: 
最后一题: 
  
     volatile char * const chptr; 
  
     Now, what is chptr?? 
     现在,chptr又是什么类型?? 
  
         ``chptr is a constant pointer to a char volatile.''  
          “chptr是一个指向char volatile型变量的指针常量。” 
  
  
Practice this rule with the examples found in K&R II on page 122. 
你可以做一个练习,用上述规则做《K&R的C语言书(第二版)》第122页上的例子。 
  
Copyright © 1993,1994 David Anderson 

抱歉!评论已关闭.