免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1495 | 回复: 1

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

论坛徽章:
0
发表于 2003-02-03 12:39 |显示全部楼层
对新手很有帮助.
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 ...

Bye.
Ankan.

论坛徽章:
0
发表于 2003-02-03 12:54 |显示全部楼层

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

厉害。

其中比较难理解的是这两种
int *p[10];
int (*p)[10];
但是如果掌握了方法就比较容易了。
其实这么多种情况,主要是考验你对优先级的理解和符号的结合。

例如下面
int *   i, j;
一般初学者(5年前的我)理解 int * 表示整数指针 i, j 是两个指针变量。
调试时老告诉我j的付值不对。弄得我只好分两行来写。
其实符号的结合应该是 int 表示整数, *i 一个指向整数的指针变量, j 一个整数变量。所以我建议写程序 *和后面的变量之间不要有空格,表明这两个是一起的。
int    *i,  j;
int    *i,  *j;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP