首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

请大家看下,关于函数指针的有关问题,多谢

2012-02-10 
请大家看下,关于函数指针的问题,谢谢。程序代码是这样的:问题在代码中间和后面。也看了很多资料,但是这些我

请大家看下,关于函数指针的问题,谢谢。
程序代码是这样的:
问题在代码中间和后面。
也看了很多资料,但是这些我还是理解不了。
请大家指点下,谢谢。
//程序是在VC++6下写的:
#include   <iostream>
using   namespace   std;
typedef   void   (*FUNC)(int);
void   print(int   i   )
{
cout   < < "this   is   func   print:   " < <i < <endl;
}
int   main(int   argc,   char*   argv[])
{
FUNC   fun   =   print;
FUNC   fun1   =   (&print);
FUNC   fun2   =   (*print);

cout   < <typeid(fun).name() < <endl;
cout   < <typeid(fun1).name() < <endl;
cout   < <typeid(fun2).name() < <endl;
//这三个输出的结果是一样的,这是为什么呢?

fun(10);
(*fun)(10);
fun1(10);
fun2(10);
//这四个输出的结果也是一样的,这里又是为什么呢?
return   0;
}
//这里,函数的名称表示的是函数的入口地址地址吗?
//函数的指针指向的也是函数的入口地址?
//如果是这样,为什么fun1,fun2,fun的类型是一样的呢?
//为什么fun(10);(*fun)(10);的结果也是一样的呢?

[解决办法]
int roundup( void ); /* Function declaration */

int *proundup = roundup;
int *pround = &roundup;

Once the function roundup is declared, two pointers to roundup are declared and initialized. The first pointer, proundup, is initialized using only the name of the function, while the second, pround, uses the address-of operator in the initialization. The initializations are equivalent.

[解决办法]
在C里面,函数的指针的付值, 应该是这样:
void (*foo)(int);
foo = &my_int_func;
foo = my_int_func;

调用的时候,可以:
(*foo)(10);
/* or */
foo(10);
这里面, foo(1,2) 是后来才兼容的, 最开始时要求(*foo)(10)的形式。

至于为啥, FUNC fun2 = (*print); 也对, 不太清楚。 可能和编译器有关(不确信)。

[解决办法]
按说,如果函数名称是函数入口地址的话,print,&print,*print,应该是不同的值才对,不知道为什么他们都是一样的。
====================================================================================
函数只有一个入口地址可以作为数值进行操作,楼主觉得还可作为别的含义吗

(CLASS.*FUNC)()格式明显是错的,(CLASS.FUNC)()或者(*CLASS.FUNC)()

热点排行