Clockwise/Spiral Rule
【Clockwise/Spiral Rule】
There is a technique known as the ``Clockwise/Spiral Rule''. (顺时针螺旋法则).
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

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的更多相关文章
- 14 Go's Declaration Syntax go语言声明语法
Go's Declaration Syntax go语言声明语法 7 July 2010 Introduction Newcomers to Go wonder why the declaration ...
- 如何阅读复杂的C类型声明
阅读复杂的C类型声明,通常采用右左法则,也就是Clockwise/Spiral Rule (顺时针/螺旋法则). 本文将首先介绍工具(cdecl)(个人比较偏好使用工具提高学习和工作效率),然后中英文 ...
- C++ 变量判定的螺旋法则
C++ 中一个标识符配合着各种修饰界定符,使得标识符的本意不那么直观一眼就能看出,甚至需要仔细分析,才能知道该标识符的具体你含义. 比如: void (*signal(int, void (*fp)( ...
- Go's Declaration Syntax
Introduction Newcomers to Go wonder why the declaration syntax is different from the tradition estab ...
- Go 开发关键技术指南 | 敢问路在何方?(内含超全知识大图)
作者 | 杨成立(忘篱) 阿里巴巴高级技术专家 Go 开发关键技术指南文章目录: 为什么你要选择 Go? Go 面向失败编程 带着服务器编程金刚经走进 2020 年 敢问路在何方? Go 开发指南大图 ...
- 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 ...
- 1105. Spiral Matrix (25)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...
- Square spiral
Square spiral Nikola picks up a strange circuit board. All of its elements are connected in a spiral ...
- PAT1105:Spiral Matrix
1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This ti ...
随机推荐
- NBA篮球足球在线直播插件下载
PPlive:点此下载PPLive播放器 Sopcast:点此下载Sopcast播放器 UUSee:点此下载UUSee播放器 CCTVReg:点此下载CCTV插件 PPStream:点此下载PPstr ...
- <mvc:annotation-driven/>的作用
<mvc:annotation-driven>的作用是: 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapt ...
- PAT 甲级 1154 Vertex Coloring
https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552 A proper vertex color ...
- 《简明Python教程》学习笔记
<简明Python教程>是网上比较好的一个Python入门级教程,尽管版本比较老旧,但是其中的基本讲解还是很有实力的. Ch2–安装Python:下载安装完成后,在系统的环境变量里,在Pa ...
- java poi给sheet表格中的某个单元格添加批注
Label l = , , "A cell with a comment"); WritableCellFeatures cellFeatures = new WritableCe ...
- vue-cli配置axios
1. npm install axios --save 2. npm install @type/axios --save-dev(使用ts编写的需要此声明文件,升级的axios好像不需要了,已经自带 ...
- Django知识总汇
基础 Django基础 Django基本命令 model系统 ORM基础 ORM字段和参数 ORM对数据库操作 ORM中介模型 ORM之其他骚操作 templates系统 模板语言 views系统 视 ...
- 从商用到开源:15个维度,全面剖析DB2与MySQL数据库的差异
随着MySQL数据库的应用越来越广泛,DB2向MySQL数据库的迁移需求也越来越多.进行数据库之间迁移的时候,首先遇到的并且也是最基本最重要的就是两种数据库数据类型之间的转换. 相关阅读: 从商用到开 ...
- 【BZOJ2962】序列操作(线段树)
[BZOJ2962]序列操作(线段树) 题面 BZOJ 题解 设\(s[i]\)表示区间内选择\(i\)个数的乘积的和 考虑如何向上合并? \(s[k]=\sum_{i=0}^klson.s[i]*r ...
- 响应式开发(六)-----Bootstrap CSS----------Bootstrap文本排版
Bootstrap 使用 Helvetica Neue. Helvetica. Arial 和 sans-serif 作为其默认的字体栈.使用 Bootstrap 的排版特性,您可以创建标题.段落.列 ...