Clockwise/Spiral Rule

Clockwise/Spiral Rule

  There is a technique known as the ``Clockwise/Spiral Rule‘‘. (顺时针螺旋法则).

  There are three simple steps to follow:

  1. 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...
  2. Keep doing this in a spiral/clockwise direction until all tokens have been covered.
  3. Always resolve anything in parenthesis first!

Example #1: Simple declaration

  

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

  

  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‘‘

    

  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.‘‘

参考:http://c-faq.com/decl/spiral.anderson.html

时间: 2024-10-25 18:09:30

Clockwise/Spiral Rule的相关文章

C 语言的 const 指针,指针的const有什么不同

Read it backwards (as driven by Clockwise/Spiral Rule)... int* - pointer to int int const * - pointer to const int int * const - const pointer to int int const * const - const pointer to const int Now the first const can be on either side of the type

顺时针/螺旋式规则 理解C/C++复杂定义

本文译自 spiral rule,后附全文. 顺时针/螺旋式规则 顺时针/螺旋式规则是一种能让任何C程序员理解程序声明的方法.如下3个步骤:1.从要确定类型的元素开始,按顺时针方向把下面遇到的元素替换为相应的语句,例如:[X] or [] ==>Array X size of ... or Array undefined size of ..;(type1,type2) ==>function passing type1 and type2 returning... * ==>point

14 Go's Declaration Syntax

Go's Declaration Syntax 7 July 2010 Introduction Newcomers to Go wonder why the declaration syntax is different from the tradition established in the C family. In this post we'll compare the two approaches and explain why Go's declarations look as th

C++ 变量判定的螺旋法则

C++ 中一个标识符配合着各种修饰界定符,使得标识符的本意不那么直观一眼就能看出,甚至需要仔细分析,才能知道该标识符的具体你含义. 比如: void (*signal(int, void (*fp)(int)))(int); 其中 signal 是什么? 螺旋法则 对于如何进行变量的辩识,有个非官方的 "顺时针/螺旋法则(Clockwise/Spiral Rule)" 可用来帮助辩识. 该法则的内容,简单来说,为了搞清楚一个未知标识符的含义,我们可以: 从我们需要判定的标识符开始,顺时

Square spiral

Square spiral Nikola picks up a strange circuit board. All of its elements are connected in a spiral and it is possible to connect the neighboring elements vertically and horizontally. The map of the circuit consists of a series of square cells. The

1105. Spiral Matrix (25)

This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n c

URAL 1224. Spiral (规律)

1224. Spiral Time limit: 1.0 second Memory limit: 64 MB A brand new sapper robot is able to neutralize mines in a rectangular region having integer height and width (N and M respectively). Before the robot begins its work it is placed near the top le

1105. Spiral Matrix (25)【模拟】——PAT (Advanced Level) Practise

题目信息 1105. Spiral Matrix (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left co

PAT1105:Spiral Matrix

1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrixis filled in from the first elem