前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >资料 | 使用Clockwise/Spiral Rule技巧轻松读懂变量/函数声明

资料 | 使用Clockwise/Spiral Rule技巧轻松读懂变量/函数声明

作者头像
好好学SLAM
发布2022-03-31 20:18:35
5430
发布2022-03-31 20:18:35
举报

最近在翻看stackoverflow时看到了一篇文章[2],主要介绍了const int*, const int * const, int const的区别,排名第一的答案中引用了一种叫Clockwise/Spiral Rule的技巧(David Anderson于1994年05月06日写的一篇博客),笔者仔细看了下,感觉这个方法非常棒,有必要给各位同学分享一下。

通过阅读本文,编程者可以很方便地搞清楚C/C++变量或者函数声明。

规则

Step 1. 从未知的变量开始,以“螺旋状+顺时针”方向移动,当遇到以下内容时,用相应的英文语句来代替它们:

代码语言:javascript
复制
[X] or []
=> Array X size of... or Array undefined size of...

(type1, type2)
=> function passing type1 and type2 returning...

*
=> pointer(s) to...

Step 2. 重复Step 1,直到所有的符号都被遍历;

Step 3. 一定要先解决括号里的东西!

例1: 简单声明

代码语言:javascript
复制
     +-------+
     | +-+   |
     | ^ |   |
char *str[10];
 ^   ^   |   |
 |   +---+   |
 +-----------+

首先问自己的一个问题:str是什么玩意?

str is an ...

  • 我们从str开始,以螺旋式顺时针方向移动,看到的第一个字符是[,所以,这意味着这里有一个数组,所以...

str is an array 10 of...

  • 继续以螺旋式的顺时针方向前进,我们遇到的下一符号是*,所以,这意味着这里有个指针,所以...

str is an array 10 of pointers to...

  • 继续沿螺旋方向前进,我们到了行的末端(;),继续前进得到了char类型,所以...

str is an array 10 of pointers to char

  • 现在已经访问了每一个符号,搞定!

例2: 指向函数声明的指针

代码语言:javascript
复制
     +--------------------+
     | +---+              |
     | |+-+|              |
     | |^ ||              |
char *(*fp)( int, float *);
 ^   ^ ^  ||              |
 |   | +--+|              |
 |   +-----+              |
 +------------------------+

首先问自己的一个问题:fp是什么玩意?

fp is a ...

  • 我们从fp开始,以螺旋式顺时针方向移动,看到的第一个字符是),因此fp在括号内,所以在括号内继续螺旋式移动,看到的下一个字符是*,所以...

fp is a pointer to...

  • 我们离开了小括号,继续以螺旋式顺时针方向前进,我们看到了(;因此,我们遇到了一个函数,所以...

fp is a pointer to a function passing an int and a pointer to float returning...

  • 继续以螺旋方式进行,然后我们看到 * 字符,所以...

fp is a pointer to a function passing an int and a pointer to float returning a pointer to...

  • 以螺旋的方式继续,我们看到了;,但还没有访问所有的符号,所以继续,最后到了char类型,所以...

fp is a pointer to a function passing an int and a pointer to float returning a pointer to char

  • 现在已经访问了每一个符号,搞定!

例3: 终极挑战!

代码语言:javascript
复制
      +-----------------------------+
      |                  +---+      |
      |  +---+           |+-+|      |
      |  ^   |           |^ ||      |
void (*signal(int, void (*fp)(int)))(int);
 ^    ^      |      ^    ^  ||      |
 |    +------+      |    +--+|      |
 |                  +--------+      |
 +----------------------------------+

首先问自己的一个问题:signal是什么玩意?(注意,变量signal在括号内,所以我们必须先搞定它)

signal is a ...

  • 沿着顺时针方向移动,我们看到(,所以有...

signal is a function passing an int and a ...

  • 额,函数传参的第二个变量有点复杂;那我们可以对fp使用同样的规则,所以...fp是什么玩意?fp也在小括号内,所以继续,我们看到一个*,所以...

fp is a pointer to...

  • 继续沿顺时针方向螺旋上升,我们得到了(,所以我们知道遇到了函数...

fp is a pointer to a function passing int returning...

  • 现在继续走出函数的小括号,我们看到了void,所以...

fp is a pointer to a function passing int returning nothing(void)

  • 我们已经完成了解析fp,所以让我们继续解析signal,我们现在有...

signal is a function passing an int and a pointer to a function passing int returning nothing(void)

  • 注意到我们仍然在括号内,所以看到的下一个字符是 *,所以...

signal is a function passing an int and a pointer to a function passing int returning nothing returning a pointer to...

  • 我们现在已经解决了括号内的符号,所以继续按顺时针方向,我们看到另一个(,我们知道遇到了函数,所以....

signal is a function passing an int and a pointer to a function passing int returning nothing returning a pointer to a function passing int returning...

  • 最后我们继续,唯一剩下的就是void,所以signal的最终完整定义是...

signal is a function passing an int and a pointer to a function passing int returning nothing returning a pointer to a function passing int returning nothing(void)

  • 现在已经访问了每一个符号,搞定!

其它

这个规则同样适用于const 以及 volatile,例如:

代码语言:javascript
复制
const char *ptr; 

ptr is a pointer to a char constant (ptr 是指向 const char 的指针,即ptr指向的值不可改变,但是指针可以改变)

代码语言:javascript
复制
char const *ptr; 

ptr is a pointer to a constant char

所以 const char *ptrchar const * ptr表示一个意思[2]。

代码语言:javascript
复制
char * const ptr;

ptr is a constant pointer to char (ptr 是一个const指针指向char,即ptr不可改变,但是指针指向的内容可以改变)

代码语言:javascript
复制
volatile char * const ptr;

ptr is a constant pointer to char volatile

参考

  1. David Anderson, Clockwise/Spiral Rule, http://c-faq.com/decl/spiral.anderson.html, 1994.
  2. stackoverflow, What is the difference between const int*, const int * const, and int const *?
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 计算机视觉SLAM 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 规则
  • 例1: 简单声明
  • 例2: 指向函数声明的指针
  • 例3: 终极挑战!
  • 其它
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档