How to understand this pointer of function

btsy2000 2009-01-21 09:58:24
doube(*)() (*e)[9]
is there somebody can explain this function pointer for me , i don't understand how to use this . thx
...全文
268 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
btsy2000 2009-01-29
  • 打赏
  • 举报
回复
thx joshua0137
捕鲸叉 2009-01-28
  • 打赏
  • 举报
回复
函数代码存在内存中,所以可以用指针指向它,如同可以用指针指向数据一样。
函数要运行有参数入栈及返加值以及调用结束后的栈平衡的问题,所以函数指针
也要反映这些信息。简单的说,函数指针是有类型信息的指向(代码)内存某个地址
的一个32位整数(在32位CPU上)
楼主说的应该是个函数指针数组,有9个指针,它们指向的函数类型无形参,返回double
向良玉 2009-01-23
  • 打赏
  • 举报
回复
like zhis
very good example:
[Quote=引用 1 楼 Kenmark 的回复:]
it's invalid~
eg.
double (*[9])() stands for an array of 9 items of pointer to function and the function takes no parameters and returns double
double (*fn(int, int))() defines a function which returns a pointer to function and take two integers as parameters and the return type is the same of example1
...
[/Quote]
jixingzhong 2009-01-22
  • 打赏
  • 举报
回复
摘录的别人的:

C语言所有复杂的指针声明,都是由各种声明嵌套构成的。如何解读复杂指针声明呢?右左法则是一个既著名又常用的方法。不过,右左法则其实并不是C标准里面的内容,它是从C标准的声明规定中归纳出来的方法。C标准的声明规则,是用来解决如何创建声明的,而右左法则是用来解决如何辩识一个声明的,两者可以说是相反的。右左法则的英文原文是这样说的:

The right-left rule: Start reading the declaration from the innermost parentheses, go right, and then go left. When you encounter parentheses, the direction should be reversed. Once everything in the parentheses has been parsed, jump out of it. Continue till the whole declaration has been parsed.


这段英文的翻译如下:

右左法则:首先从最里面的圆括号看起,然后往右看,再往左看。每当遇到圆括号时,就应该掉转阅读方向。一旦解析完圆括号里面所有的东西,就跳出圆括号。重复这个过程直到整个声明解析完毕。

笔者要对这个法则进行一个小小的修正,应该是从未定义的标识符开始阅读,而不是从括号读起,之所以是未定义的标识符,是因为一个声明里面可能有多个标识符,但未定义的标识符只会有一个。

现在通过一些例子来讨论右左法则的应用,先从最简单的开始,逐步加深:

int (*func)(int *p);

首先找到那个未定义的标识符,就是func,它的外面有一对圆括号,而且左边是一个*号,这说明func是一个指针,然后跳出这个圆括号,先看右边,也是一个圆括号,这说明(*func)是一个函数,而func是一个指向这类函数的指针,就是一个函数指针,这类函数具有int*类型的形参,返回值类型是int。

int (*func)(int *p, int (*f)(int*));

func被一对括号包含,且左边有一个*号,说明func是一个指针,跳出括号,右边也有个括号,那么func是一个指向函数的指针,这类函数具有int *和int (*)(int*)这样的形参,返回值为int类型。再来看一看func的形参int (*f)(int*),类似前面的解释,f也是一个函数指针,指向的函数具有int*类型的形参,返回值为int。

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

func右边是一个[]运算符,说明func是一个具有5个元素的数组,func的左边有一个*,说明func的元素是指针,要注意这里的*不是修饰func的,而是修饰func[5]的,原因是[]运算符优先级比*高,func先跟[]结合,因此*修饰的是func[5]。跳出这个括号,看右边,也是一对圆括号,说明func数组的元素是函数类型的指针,它所指向的函数具有int*类型的形参,返回值类型为int。


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

func被一个圆括号包含,左边又有一个*,那么func是一个指针,跳出括号,右边是一个[]运算符号,说明func是一个指向数组的指针,现在往左看,左边有一个*号,说明这个数组的元素是指针,再跳出括号,右边又有一个括号,说明这个数组的元素是指向函数的指针。总结一下,就是:func是一个指向数组的指针,这个数组的元素是函数指针,这些指针指向具有int*形参,返回值为int类型的函数。

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

func是一个函数指针,这类函数具有int*类型的形参,返回值是指向数组的指针,所指向的数组的元素是具有5个int元素的数组。

要注意有些复杂指针声明是非法的,例如:

int func(void) [5];

func是一个返回值为具有5个int元素的数组的函数。但C语言的函数返回值不能为数组,这是因为如果允许函数返回值为数组,那么接收这个数组的内容的东西,也必须是一个数组,但C语言的数组名是一个右值,它不能作为左值来接收另一个数组,因此函数返回值不能为数组。

int func[5](void);

func是一个具有5个元素的数组,这个数组的元素都是函数。这也是非法的,因为数组的元素除了类型必须一样外,每个元素所占用的内存空间也必须相同,显然函数是无法达到这个要求的,即使函数的类型一样,但函数所占用的空间通常是不相同的。

作为练习,下面列几个复杂指针声明给读者自己来解析,答案放在第十章里。

int (*(*func)[5][6])[7][8];

int (*(*(*func)(int *))[5])(int *);

int (*(*func[7][8][9])(int*))[5];

实际当中,需要声明一个复杂指针时,如果把整个声明写成上面所示的形式,对程序可读性是一大损害。应该用typedef来对声明逐层分解,增强可读性,例如对于声明:

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

可以这样分解:

typedef int (*PARA)[5];
typedef PARA (*func)(int *);

这样就容易看得多了。
btsy2000 2009-01-22
  • 打赏
  • 举报
回复
OK,thx guys, I will end tomorrow!
ForestDB 2009-01-22
  • 打赏
  • 举报
回复
Firstly, you could understand the complicated pointer declaration, such as
double (*e[9])(); // 见星羽的代码
"e" is an array having 9 elements, each of which is a pointer to a function, this function should take no parameter and return a double.
Then, you could used "typedef" to simplify the declaration, such as
typedef double(*t)(); // here "t" is of type of a pointer to a function which take no parameter and return double
t e[9]; // "e" is just same as above

#include <stdio.h>

double f1() { return 1.0; }
double f2() { return 2.0; }
double f3() { return 3.0; }
double f4() { return 4.0; }
double f5() { return 5.0; }
double f6() { return 6.0; }
double f7() { return 7.0; }
double f8() { return 8.0; }
double f9() { return 9.0; }

int main(void)
{
double (*e[9])() = { f1, f2, f3, f4, f5, f6, f7, f8, f9 };
int i;

for(i = 0; i < 9; ++i)
printf("%d: %f\n", i, e[i]());

return 0;
}


#include <stdio.h>

double f1() { return 1.0; }
double f2() { return 2.0; }
double f3() { return 3.0; }
double f4() { return 4.0; }
double f5() { return 5.0; }
double f6() { return 6.0; }
double f7() { return 7.0; }
double f8() { return 8.0; }
double f9() { return 9.0; }

typedef double (*t)();

int main(void)
{
t e[9] = { f1, f2, f3, f4, f5, f6, f7, f8, f9 };
int i;

for(i = 0; i < 9; ++i)
printf("%d: %f\n", i, e[i]());

return 0;
}
nullah 2009-01-22
  • 打赏
  • 举报
回复
just use it and read example
雷少爷爷 2009-01-22
  • 打赏
  • 举报
回复
up
blueness21 2009-01-22
  • 打赏
  • 举报
回复
我路过。。看大侠飙E文
hityct1 2009-01-22
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <iostream>
using namespace std;

//直接定于typedef doube(*)() (*e)[2];不行,但用以下两句代替:
typedef double (*pFun)();//定义函数指针pFun
typedef pFun (*e)[2];//定义型别e,是个指针

double Fun1()
{
cout<<"Fun1"<<endl;
return 1;
};

double Fun2()
{
cout<<"Fun2"<<endl;
return 2;
};

int main()
{
pFun array[2]= {Fun1, Fun2};
array[0]();//执行Fun1
array[1]();//执行Fun2

//示例1
e MyE = &array;//将array的首地址付给MyE
cout<<sizeof(MyE)<<endl;//既然是指针,长度自然为4(32位机上)
cout<<sizeof(*MyE)<<endl;//(两个指针)长度为8(32位机上)

(*MyE[0])();//执行Fun1,注意优先级,其实就是 (*(MyE[0]))();
//(*MyE[1])();//此句将导致异常,很简单地址位移了8,指到array后面了。注意MyE[1];等价于MyE++;

(*MyE)[0]();//执行Fun1
(*MyE)[1]();//执行Fun2

//cout<<"--------------------------"<<endl;

return 0;
}
chin_chen 2009-01-22
  • 打赏
  • 举报
回复
那个作者显然错了啊,编译通不过的 :abstract declarator `double (*)()' used as declaration ;
typedef double(*ptr)();
ptr e[9];

这样可以的
tanmeining 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 btsy2000 的回复:]
how to use this function?
[/Quote]
it's a pointer of function
double (* e[9])();
it is express that a function return a array of function pointer,it has 9 elements.

How to used?
May be you can like 5 floor...
it's perfect,OK?
sanguomi 2009-01-21
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;

int fun(int a)
{
return a;
}

typedef
int(*funname)(int a);

typedef
int(*funarray[3])(int a);

int main()
{
int (*test[1])(int a) = {fun};
cout<<test[0](2)<<endl;
funname test1 = fun;
cout<< test1(3)<<endl;

funarray test2 = {fun};
cout<<test2[0](4)<<endl;

return 0;
}
waizqfor 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 btsy2000 的回复:]
how to use this function?
[/Quote]
double (* e[9])() is true
doube(*)() (*e)[9] can't use (no way)
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 btsy2000 的回复:]
how to use this function?
[/Quote]

there are some examples upstairs, you can see floor 3 and floor 5
btsy2000 2009-01-21
  • 打赏
  • 举报
回复
how to use this function?
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 btsy2000 的回复:]
by the way I know how to define and use function pointer, I just can't analyze this "doube(*)() (*e)[9]".
this is author's wrong or others?

[/Quote]

"doube(*)() (*e)[9]" is wrong
it should be "double (*e[9])()"
btsy2000 2009-01-21
  • 打赏
  • 举报
回复
by the way I know how to define and use function pointer, I just can't analyze this "doube(*)() (*e)[9]".
this is author's wrong or others?
waizqfor 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xiaoyisnail 的回复:]
C/C++ code
typedef double(*pFuny)();
typedef pFuny (*pFunParamy)[9];
pFunParamy e;



this is ok.
use typedef to simplify the declaration
[/Quote]
UP
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复

typedef double(*pFuny)();
typedef pFuny (*pFunParamy)[9];
pFunParamy e;

this is ok.
use typedef to simplify the declaration
加载更多回复(7)

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧