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:

  1. const char *chptr;
  • Now, what is chptr??

    ``chptr is a pointer to a char constant''

How about this one:

  1. char * const chptr;
  • Now, what is chptr??

    ``chptr is a constant pointer to char''

Finally:

  1. 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. POJ2823(单调队列方法解题)

    因为不太好复制,我就直接截图了,题目链接:题目大致的意思是:给一串数字,然后要你求出每k长度的连续子序列中的最大值以及最小值并输出:这题就是一个最简单的运用单调队列方法解题的例子. 解题思路:通过单调 ...

  2. 最新版ABP 动态WebAPI 日期转json带T的解决方案| ABP DateTIme Json format

    ABP动态webapi返回的json数据中,日期时间带T还有毫秒数的问题,在以往的版本中可以使用下面方法解决: 在XXXAbpWebApiModule中加上下面的代码: 很老的很老的版本有效: pub ...

  3. vue 实战 遇到问题记录

    vue-router  配置路由遇到问题 1.一个 new Router({ routes:[ { path:'/', component:Good    ///不要写成components  否则报 ...

  4. Linux内核0.11 makefile文件说明

    # # if you want the ram-disk device, define this to be the # size in blocks. # 如果要使用 RAM 就定义块的大小(注释掉 ...

  5. 使用Shell脚本删除/清空日志文件

    话不多少,直接上代码: #!/bin/bash workdir=("/home/Tax_Punish_Ret/log_txt") #可填写多个路径, 用空格隔开 # 查找日志文件 ...

  6. Nagios学习笔记

    1 Nagios功能 1.1  监控工具 1.2  可以监控主机/服务或者资源 1.3  四种状态值 OK,WARNING,CRITICAL,UNKNOWN CPU:90%(CRITICAL),80% ...

  7. CSS实现点击改变元素背景色

    可通过使用css伪类实现点击元素变色的效果,两个伪类是:active, :focus :active :active选择器用于选择活动链接.当在一个链接上点击时,它就会成为活动的(激活的),:acti ...

  8. SPOJ Triple Sums(FFT+容斥原理)

    # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream& ...

  9. Codeforces 1060 F. Shrinking Tree

    题目链接 一道思维好题啊...感觉这种类型的题很检验基本功是否扎实(像我这样的就挂了). 题意:你有一棵\(n\)个点的树,每次随机选择一条边,将这条边的两个端点合并,并随机继承两个点标号中的一个,问 ...

  10. java 连接oracle 进行增删改查

    1.在DAO层新增类OraclePersionDao package com.test.dao; import java.sql.*; /** * Created by wdw on 2017/9/1 ...