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. Head First Java & static

  2. SQL语句中的output用法

    private void button2_Click(object sender, RoutedEventArgs e) { using (SqlConnection conn = new SqlCo ...

  3. Sprint1回顾

    Sprint目标 此产品为适用于小学生使用的四则运算训练软件.关于第一期Sprint冲刺的目标,我们打算实现产品的以下几点的功能: •1.初始界面设计 •2.四则基本运算算法 •3.能产生随机式子 • ...

  4. sqlserver trigger(触发器)-更新某几列数据时触发【转】

    CREATE TRIGGER [dbo].[updataAlarmLevel]ON [dbo].[Alarm_Alarm_Info]AFTER INSERT, UPDATE – 在更新和插入之后ASB ...

  5. vue组件讲解(is属性的用法)

    什么是组件? 在说之前我们先搞清楚什么是组件?这样对我们下边的学习是很有帮助的. 组件(Component)是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可以重复使用的代码.在较高层次 ...

  6. Java-JDBC.mysql 工具类 读取本地文件配置

    引用 mysql-connector-jav 的jar 配置文件为  database.propertties .  格式如下 driverClass=com.mysql.jdbc.Driver ur ...

  7. (暂时弃坑)ACM数论之旅15---置换群与Polya定理(我把标题看成poi了,poipoipoi(*≧▽≦)ツ)

    (挖坑...) ////////////////////////////////////////////////// 暂时弃坑 开学了,有空再写....

  8. C语言版kafka消费者代码运行时异常kafka receive failed disconnected

    https://github.com/edenhill/librdkafka/wiki/Broker-version-compatibility如果使用了broker版本是0.8的话, 在运行例程时需 ...

  9. js & get recursive ids

    js & get recursive ids len = 0; bug for(let i = 0; i < 3; i++) { console.log(`i =`, i); let y ...

  10. BZOJ5118 Fib数列2(矩阵快速幂)

    特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...