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. 解决tomcat登录需要给角色授权

    1:编辑/usr/local/tomcat/conf/tomcat-users.xml文件,在没有注释的内容中添加: <role rolename="manager-gui" ...

  2. jmeter body 中文显示为乱码解决

    这种情况在jmeter3.0的版本中才会产生,注意:这不是乱码,而是由于3.0中优化body data后,使用默认的字体(Consolas)不支持汉字的显示.这样的情况可以这样调整:进入jmeter. ...

  3. Appium 服务关键字(转)

    来源: https://github.com/appium/appium/blob/master/docs/cn/writing-running-appium/caps.cn.md#appium-服务 ...

  4. Dubbo学习(二) Dubbo 集群容错模式-负载均衡模式

    Dubbo是Alibaba开源的分布式服务框架,我们可以非常容易地通过Dubbo来构建分布式服务,并根据自己实际业务应用场景来选择合适的集群容错模式,这个对于很多应用都是迫切希望的,只需要通过简单的配 ...

  5. 惭愧, eclipse 之 build path

    算下来大学到现在已近用了很久的 eclipse 了, 包括 myeclipse, 但是今天碰到的问题让我很惭愧, 一个老项目的编译都搞了好久. 环境: Myeclipse 6.X Struts 1.X ...

  6. Jmeter—添加断言 判断接口响应数据是否符合预期

    发出请求之后,通过添加断言可以判断响应数据是否是我们的预期结果. 1 在Jmeter中发送一个状态返回200的http请求(参数故意输入错误).结果肯定是不是返回200啦. 但结果树中http请求的图 ...

  7. 【BZOJ3675】【Apio2014】序列分割

    Description 传送门 Solution ​ 之前我也遇到过一次这种"两段之和乘积作为贡献"的问题:考虑把这一种\((\sum) *(\sum)\)的形式拆括号,就可以发现 ...

  8. AC自动机【萌新文章】

    我这个蒟蒻第一次写博客,有点小激动呢. 主要是最近刚学了AC自动机,学得糟糟糕糕,记录一下,看到dalao们都在写博客,决定自己也写一波[我好水的啦,写的也不好] AC自动机大概就是    Trie+ ...

  9. Android Studio aidl文件路径自定义问题

    1.aidl旧文件夹中添加的内容无法编译 sourceSets中主要是把把src/main/aidl文件也作为java.srcDirs, resources.srcDirs,这样当编译程序时,AIDL ...

  10. 「转」图像算法---白平衡AWB

    本文大体讲解了白平衡的算法流程,适用于想了解和学习白平衡原理的筒子们. 一般情况下要实现AWB算法需要专业的图像和算法基础,本文力图通过多图的方式,深入浅出,降低初学者理解上的门槛,让大家都理解到白平 ...