免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2008 | 回复: 12
打印 上一主题 下一主题

What is the declaration int *((*(*p[5])()))[10]; [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-12-27 13:59 |只看该作者 |倒序浏览
One reason why I wrote this FAQ is that I did not find one dealing with such a controversial (and sometimes confusing) topic as this in the forum. Another (perhaps the main reason!!) is that I had not written any FAQ yet .

If you ever had difficulties dealing with pointers (you would have certainly had, if you are a C programmer) then go through this FAQ. Otherwise don't bother.

In the following declarations, p is ...
int *p;        pointer to int
int *p[10];    array[10] of pointers to int (not same as a 2D array)
int (*p)[10];  pointer to array[10] of int
int *p();      function returning pointer to int
int (*p)();    pointer to function returning int
int *(*p)();   pointer to function returning pointer to int
int (*p[])();  array[] of pointers to function returning int
int (*(*p())[5])(); function returning pointer to array[5] of pointers to function returning int

The above become quite clear when we consider the precedence and associativity of operators:
   () [] {left to right}
   *     {right to left}

As an example what is p in the declaration:
int *((*(*p[5])()))[10];

One nice and easy way to come to the correct conclusion is by assuming that you are the compiler and going through the following obvious steps. Obvious when you keep in mind the precedence and associativity of the operators at hand that is [], ( ), *.

First we have p[5] which is obviously an array, so we have
array[5] of

Next is *p[5], which is a pointer, thus
array[5] of pointers to

Then (*p[5])(), which represents a function, hence
array[5] of pointers to function returning

Then *(*p[5])(), a pointer hence
array[5] of pointers to function returning pointer to

Next consider ((*(*p[5])()))[10], which is an array, so we get
array[5] of pointers to function returning pointer to array[10] of
(an extra pair of ( ) above is of no use although it does no harm)

Then comes *((*(*p[5])()))[10], which is a pointer, so we get
array[5] of pointers to function returning pointer to array[10] of pointers to

Lastly, the type which is int in this case which ultimately leads us to
array[5] of pointers to function returning pointer to array[10] of pointers to int

When you are writing code all you have to do is to follow the reverse of the above steps, which is fairly easy.

Such a complicated (?? if you have gone through the above, it should seem too simple now) pointer/array/function/etc is seldom used, I think.

And a nicer and more foolproof way to use such a thing is by using typedef. But that's another story ...

论坛徽章:
0
2 [报告]
发表于 2003-12-27 14:12 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

对于我来说这部分还没有内化,再学学

论坛徽章:
0
3 [报告]
发表于 2003-12-27 14:24 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

int *((*(*p[5])()))[10]; 这个东西也许一生也不会碰到。

论坛徽章:
0
4 [报告]
发表于 2003-12-27 20:27 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

不想看英文的过来:

int *((*(*p[5])()))[10];

p 是一个数组,大小为5。这个数组的元素为函数指针。这些指针指向的函数参数未定,其返回值是一个指针,这个指针指向一个大小为10的数组,数组的元素是整数指针。

实际应用中也有类似复杂的定义,unix中信号函数就是其中一个。这些是c程序员的噩梦。

论坛徽章:
0
5 [报告]
发表于 2003-12-29 11:45 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

这种自虐题挺有意思的.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2003-12-29 12:00 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

建议看一下《c++编程思想》,里面有一段讲怎么分析
这种复杂的定义,说得方法很好,学会后以后再看到
这些东西理解起来就容易多了。呵呵,等我找到了,
我给贴上来。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2003-12-29 12:05 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

找到了,贴上,大家参考一下。
    试问double (*(*(*fp3)())[10])() 定义的是什么??这是什么复杂的定义哟?谁遇上这样的定义都会感到费解的。我们来看看书中介绍方法——通过遵循编译器分析“定义”的法则:从变量名开始,先右后左,遇括号返回。
??double ( * ( * ( * fp3 ) ( ) ) [10] ) ( )
??变量名为fp3,向右探测遇括号返回,向左发现*,故fp3是指针;再向右发现一对空括号,遇右括号返回,向左发现*,该指针指向一个函数(函数无参数,返回指针);再向右发现[10],向左发现*,函数返回的指针指向一个10元素指针数组;再向右是(),向左是double,指针数组的指针指向一个无参数返回double类型数据的函数。

论坛徽章:
0
8 [报告]
发表于 2003-12-29 13:19 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

lenovo兄,昨天晚上俺刚看了C++编程思想里这一段
当时似乎明白了
放下书又忘记了,hoho
看来还得自己用过才能彻底明白。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
9 [报告]
发表于 2003-12-29 14:10 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

原帖由 "unicorns" 发表:
lenovo兄,昨天晚上俺刚看了C++编程思想里这一段
当时似乎明白了
放下书又忘记了,hoho
看来还得自己用过才能彻底明白。

是呀,如果自己没用过,可能别人一解释就能明白,
但自己去定义这么复杂的东西,估计就不行了。

论坛徽章:
0
10 [报告]
发表于 2003-12-29 15:09 |只看该作者

What is the declaration int *((*(*p[5])()))[10];

呵呵,谁要这么用的话绝对强~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP