顺时针方法 解读 C 复杂定义

[This was posted to comp.lang.c by its author, David Anderson, on 1994-05-06.]

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:

  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

                     +-------+
                     | +-+   |
                     | ^ |   |
                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.



Copyright 1993,1994 David Anderson

This article may be freely distributed as long as the author's name and this notice are retained.

时间: 2024-09-24 00:46:53

顺时针方法 解读 C 复杂定义的相关文章

iOS中UITextView方法解读

iOS中UITextView方法解读 常用属性解读: @property(nonatomic,assign) id<UITextViewDelegate> delegate; 设置代理属性 @property(nonatomic,copy) NSString *text; textView上的文本 @property(nonatomic,retain) UIFont *font; 设置文本字体 @property(nonatomic,retain) UIColor *textColor; 设置

java类的问题-方法内的局部变量定义时必须进行初始化吗?

问题描述 方法内的局部变量定义时必须进行初始化吗? 书上看到方法内定义的变量为局部变量包括形参,使用时必须进行初始化,否则编译器报错.但我在main方法内去定义变量并没有初始化,编译器也可以编辑不报错 解决方案 使用时必须进行初始化 你只是定义当然没错阿 定义和使用是不一样的哦 你试试用它调用一个方法或者使用以下你定义的,你看看 解决方案二: 只是定义,没有必要初始化:除非你定义 const 型的变量,它是必须在定义时初始化的,因为定义后它的值无法修改. 解决方案三: 局部变量定义和初始化两个概

api-如何将三层架构里的方法在MVC中定义成接口

问题描述 如何将三层架构里的方法在MVC中定义成接口 如何将三层架构里写好的增删改查方法在MVC中定义成api接口,给个具体的实例!或者写一个增删的实例.谢谢啊! 这个不会,麻烦高手指点下! 解决方案 用什么框架?用什么语言写这个api接口? http的接口就可以实现这个功能. 解决方案二: 都说了在MVC模式中中使用三层架构 定义一个API的控制器 把三层架构里的增删改查方法在API类里定义成接口了 然后前台再调用接口实现对数据的操作 不让用HTTP 要是可以的话 我就不问了 直接就弄出来了

Java源码解析Integer方法解读_java

toUnsignedString方法解读 看到Integer中有这样的一个方法把int转为Unsigned类型的字符串,但是有几个点不是很清楚,经过查询资料弄懂了,解读如下: /** * Convert the integer to an unsigned number. */ private static String toUnsignedString(int i, int shift) { char[] buf = new char[32]; int charPos = 32; int ra

Scala:重载方法和字段及定义参数化字段

重载方法和字段 统一访问原则只是Scala在对待字段和方法方面比Java更统一的一个方面.另 一个差异是Scala里,字段和方法属于相同的命名空间.这使得字段重载无参数方法成为可能.比如说, 你可以改变类ArrayElement中contents的实现,从一个方法变为一个字段,而无需修改类Element中 contents的抽象方法定义,如展示在代码10.4中的那样: class ArrayElement(conts: Array[String]) extends Element { val c

关于方法中对象的定义细节

问题描述 如何在同一个类的一个方法中调用另一个发法定义的实例对象(非成员变量)?请路过的大侠给点思路,困扰了两天了. 解决方案 本帖最后由 zhenren11 于 2011-04-01 21:09:46 编辑解决方案二:不可以吧~~~可以吗?不可以吧~~~解决方案三:非要这么做吗?不知道题意我有没有理解错,你把两个方法合起来吧解决方案四:java,不行的,交给第三方吧,哈哈.解决方案五:可以把实例对象定义成类的静态成员良好的设计可以避免很多问题.解决方案六:该回复于2011-04-02 17:0

大数据处理与分析方法解读

越来越多的应用涉及到大数据,这些大数据的属性,包括数量,速度,多样性等等都是呈现了大数据不断增长的复杂性,所以,大数据的分析方法在大数据领域就显得尤为重要,可以说是决定最终信息是否有价值的决定性因素.基于此,大数据分析的方法理论有哪些呢? 大数据分析的五个基本方面 PredictiveAnalyticCapabilities(预测性分析能力) 数据挖掘可以让分析员更好的理解数据,而预测性分析可以让分析员根据可视化分析和数据挖掘的结果做出一些预测性的判断. DataQualityandMaster

IOS UITextField 使用与方法解读

UITextField是IOS开发中用户交互中重要的一个控件,常被用来做账号密码框,输入信息框等. 初始化一个文字框: ? 1 UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 30, 100, 100)]; 设置和获取文字框文字: @property(nonatomic,copy)  NSString *text;  通过AttributedString创建和获取文字: @property(n

Javascript数组Array方法解读_基础知识

接上一篇<Javascript数组Array基础介绍>,这一篇详细介绍Array的所有方法. 所有数组的方法都定义在Array.prototype上,而Array.prototype本身也是一个数组. array.concat() 浅复制一份当前数组,并把接收到的参数附加到新数组的末尾.原数组不改变. 语法 array.concat(value1, value2, ..., valueN) 参数为需要合并的数组或非数组值 var arr1 = [1, 2, 3]; var obj = {ani