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

Clockwise/Spiral Rule的更多相关文章

  1. 14 Go's Declaration Syntax go语言声明语法

    Go's Declaration Syntax go语言声明语法 7 July 2010 Introduction Newcomers to Go wonder why the declaration ...

  2. 如何阅读复杂的C类型声明

    阅读复杂的C类型声明,通常采用右左法则,也就是Clockwise/Spiral Rule (顺时针/螺旋法则). 本文将首先介绍工具(cdecl)(个人比较偏好使用工具提高学习和工作效率),然后中英文 ...

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

    C++ 中一个标识符配合着各种修饰界定符,使得标识符的本意不那么直观一眼就能看出,甚至需要仔细分析,才能知道该标识符的具体你含义. 比如: void (*signal(int, void (*fp)( ...

  4. Go's Declaration Syntax

    Introduction Newcomers to Go wonder why the declaration syntax is different from the tradition estab ...

  5. Go 开发关键技术指南 | 敢问路在何方?(内含超全知识大图)

    作者 | 杨成立(忘篱) 阿里巴巴高级技术专家 Go 开发关键技术指南文章目录: 为什么你要选择 Go? Go 面向失败编程 带着服务器编程金刚经走进 2020 年 敢问路在何方? Go 开发指南大图 ...

  6. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

  7. 1105. Spiral Matrix (25)

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  8. Square spiral

    Square spiral Nikola picks up a strange circuit board. All of its elements are connected in a spiral ...

  9. PAT1105:Spiral Matrix

    1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This ti ...

随机推荐

  1. 基于html5的多图片上传,预览

    基于html5的多图片上传 本文是建立在张鑫旭大神的多文图片传的基础之上. 首先先放出来大神多图片上传的博客地址:http://www.zhangxinxu.com/wordpress/2011/09 ...

  2. [转帖] go的import 语法

    package 的导入语法写 Go 代码的时经常用到 import 这个命令用来导入包,参考如下: import( "fmt" ) 然后在代码里面可以通过如下的方式调用: fmt. ...

  3. equals比较对象

    object类的equals方法用来比较是否是同一个对象,比较内存地址. jdk中有些类重写了equals方法,只要类型,内容相同,就相等. 类如果涉及到比较应该重写equals方法,比较内存地址没有 ...

  4. 刷新dbgrid 而不失去当前行位置

    我们有一个Delphi的数据库应用程序,上面有个DBGrid和一个数据集: DBGrid是用来显示来自数据集(查询或表)的数据,根据设计,当您调用已经打开的数据集的Refresh方 法(例如使用DBN ...

  5. 【Python】Python基础

    源程序文件通常以.py为扩展名 #!/usr/bin/python shebang,即执行脚本时通知内容要启动的解释器 import platform 导入模块 print platform.unam ...

  6. BZOJ5212 ZJOI2018历史(LCT)

    首先相当于最大化access的轻重边交换次数. 考虑每个点作为战场(而不是每个点所代表的国家与其他国家交战)对答案的贡献,显然每次产生贡献都是该点的子树内(包括自身)此次access的点与上次acce ...

  7. laravel4 「时间戳」问题

    默认 Eloquent 会自动维护数据库表的 created_at 和 updated_at 字段.只要把这两个「时间戳」字段加到数据库表, Eloquent 就会处理剩下的工作.如果不想让 Eloq ...

  8. DSU模板(树的启发式合并)

    摘自Codeforces博客 With dsu on tree we can answer queries of this type: How many vertices in subtree of ...

  9. 使用jQuery在javascript中自定义事件

    js中的自定义事件有attachEvent,addEventListener等等好多种,往往受困于浏览器兼容,而且代码写起来也相当麻烦.jQuery为我们解决了这个问题,几行代码就可以很好的实现事件的 ...

  10. java解数独

    先输入要解的数独,采用多维数组来保存其中的值,未填数字的地方,初始化为0,然后采用递归的方法来解数独. 直接上代码: /** * * @author walker * */ public class ...