Clockwise/Spiral Rule
[This was posted to comp.lang.c by its author, David Anderson, on 1994-05-06.]
文章转自:http://c-faq.com/decl/spiral.anderson.html
The ``Clockwise/Spiral Rule''
By 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!
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
+-------+| +-+ || ^ | |char *str[10];^ ^ | || +---+ |+-----------+
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
+--------------------+| +---+ || |+-+| || |^ || |char *(*fp)( int, float *);^ ^ ^ || || | +--+| || +-----+ |+------------------------+
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''
+-----------------------------+| +---+ || +---+ |+-+| || ^ | |^ || |void (*signal(int, void (*fp)(int)))(int);^ ^ | ^ ^ || || +------+ | +--+| || +--------+ |+----------------------------------+
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.''
Practice this rule with the examples found in K&R II on page 122.
转载于:https://www.cnblogs.com/vd01/p/5703693.html
Clockwise/Spiral Rule相关推荐
- The Clockwise/Spiral Rule
This paper was posted to comp.lang.c by its author, David Anderson, on 1994-05-06. The Clockwise/Spi ...
- codewars020: The Clockwise Spiral 数字顺时针螺旋矩阵
2019独角兽企业重金招聘Python工程师标准>>> https://www.codewars.com/kata/536a155256eb459b8700077e/train/ja ...
- 【译】Go语言声明语法
引言 Go语言的初学者可能会好奇为什么Go的类型声明语法和传统的C系语言不同.本篇文章我们将比较这两种不同的类型声明方式,解释为什么Go的声明会是这样子. C语法 首先,我们谈谈C的语法.C采用了一种 ...
- Go 开发关键技术指南 | 敢问路在何方?(内含超全知识大图)
作者 | 杨成立(忘篱) 阿里巴巴高级技术专家 Go 开发关键技术指南文章目录: 为什么你要选择 Go? Go 面向失败编程 带着服务器编程金刚经走进 2020 年 敢问路在何方? Go 开发指南大图 ...
- C++ 变量判定的螺旋法则
C++ 中一个标识符配合着各种修饰界定符,使得标识符的本意不那么直观一眼就能看出,甚至需要仔细分析,才能知道该标识符的具体你含义. 比如: void (*signal(int, void (*fp)( ...
- Go 把类型放在变量名后面,是特立独行还是另有机密?
前段时间看到大家在吵一个话题,那就是 Go 语言的类型声明,抠知识抠的非常细了,就是为什么他要放在后面,展开了热烈的讨论. 示例代码如下: var a []string var b []string ...
- LINUX杂谈与系统编程
目录 黑马课程学习 文件,目录,目录项,文件描述符 用fork函数循环创建多个子进程 父进程与子进程关于全局变量 获取子进程退出值和异常终止值--进一步理解fork函数工作方式 循环遍历目录 实现ls ...
- C++/C 顺时针法则
C语言标准库有一个函数signal(),其声明让人大开眼界:void (*signal(int sig, void (*func)(int)))(int); 这种晦涩的声明实在让人难以理解,在网上搜索 ...
- PAT1105:Spiral Matrix
1105. Spiral Matrix (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue This ti ...
最新文章
- linux内核路由反向检查,反向路径过滤
- 小米手机能用上鸿蒙吗,鸿蒙系统小米手机能用吗?鸿蒙系统支持第三方手机!
- 应用深度学习(台大陈蕴侬李宏毅) Part1
- 线性表 - 数据结构和算法06
- c盘保护软件_电脑C盘空间越来越小?简单操作这几步,可以释放大量空间
- WordPress超级基本教程(转)
- ios手机怎么连接adb命令_Mac连接Adb
- UITextView - 2
- 疯狂脑机接口计划:马斯克的 “读心术”
- 干货!史上最全Java进阶好书清单来了!
- (145)光线追踪距离场柔和阴影
- python flask上传文件_Python之利用Flask上传文件、Flask_RESTful
- 调试设置移动端Web开发环境搭建实践
- JavaEE实现微博项目(含注册、登录、发表微博、评论微博、关注博主、阅读排行榜、评论排行榜等功能)
- Why That Big Meal You Just Ate Made You Hungry
- ps切片工具里没有html,PS切片工具怎么用?PS切片工具的使用方法
- [BUUCTF-pwn] wdb_2018_semifinal_pwn2
- NO PAPER COWBOYS【翻译】
- android 五子棋 布局技巧,与电脑对战「五子棋 – Gomoku」难度由浅入深,棋局练习、分析五大能力!(iPhone,Android)...
- 从μC/OS-II到μC/OS-III的各种改进