函数指针的有关问题
函数指针的问题
请看简短的程序如下:
#include <iostream.h>
int fun(int x, int y)
{
return x*y;
}
void main()
{
int (*p)(int, int) = fun; //
cout < <p(4,5) < <endl;
}
//后面改为&fun好像结果也是正确的,这是怎么回事?难道两个都对?
------解决方案--------------------
将一个函数的地址初始化或赋值给一个指向函数的指针时,无需要显式地取得函数地址,编译器知道隐式地获取函数的地址,因此在这种情况下’&’操作符是可选的,通常省略不用。
类似地,为了调用函数指针所指向的函数而对指针进行解引用操作也是不必要的,因为编译器可以帮你解引用:
(*fp)(12); // 显式地解引用(explicit dereference)
fp(12); // 隐式地解引用,结果一样(implicit dereference, same result)
------解决方案--------------------
int a[2] = {9, 10};
cout < <(a) < <endl;
cout < <(&a) < <endl;
结果一样,作何解啊?
-------------------------
你可以去艘艘一个讨论函数名的贴子
函数名是个地址,所以取地址就是它自己
------解决方案--------------------
char a[100];
char (*p)[100] = &a;
你可以在调试模式 看看 a,p 的值,是一样的
------解决方案--------------------
对函数指针,前种写法是C,后者是C++写法。由于C++兼容C,两者通用。
这里并没有歧义,因为函数取地址再取地址没有意义。
------解决方案--------------------
2.3 Assign an address to a Function Pointer
It 's quite easy to assign the address of a function to a function pointer. You simply take the name of a suitable and known function or member function. Although it 's optional for most compilers you should use the address operator & infront of the function 's name in order to write portable code. You may have got to use the complete name of the member function including class-name and scope-operator (::). Also you have got to ensure, that you are allowed to access the function right in scope where your assignment stands.
// 2.3 assign an address to the function pointer
// Note: Although you may ommit the address operator on most compilers
// you should always use the correct way in order to write portable code.
// C
int DoIt (float a, char b, char c){ printf( "DoIt\n "); return a+b+c; }
int DoMore(float a, char b, char c)const{ printf( "DoMore\n "); return a-b+c; }
pt2Function = DoIt; // short form
pt2Function = &DoMore; // correct assignment using address operator
请看简短的程序如下:
#include <iostream.h>
int fun(int x, int y)
{
return x*y;
}
void main()
{
int (*p)(int, int) = fun; //
cout < <p(4,5) < <endl;
}
//后面改为&fun好像结果也是正确的,这是怎么回事?难道两个都对?
------解决方案--------------------
将一个函数的地址初始化或赋值给一个指向函数的指针时,无需要显式地取得函数地址,编译器知道隐式地获取函数的地址,因此在这种情况下’&’操作符是可选的,通常省略不用。
类似地,为了调用函数指针所指向的函数而对指针进行解引用操作也是不必要的,因为编译器可以帮你解引用:
(*fp)(12); // 显式地解引用(explicit dereference)
fp(12); // 隐式地解引用,结果一样(implicit dereference, same result)
------解决方案--------------------
int a[2] = {9, 10};
cout < <(a) < <endl;
cout < <(&a) < <endl;
结果一样,作何解啊?
-------------------------
你可以去艘艘一个讨论函数名的贴子
函数名是个地址,所以取地址就是它自己
------解决方案--------------------
char a[100];
char (*p)[100] = &a;
你可以在调试模式 看看 a,p 的值,是一样的
------解决方案--------------------
对函数指针,前种写法是C,后者是C++写法。由于C++兼容C,两者通用。
这里并没有歧义,因为函数取地址再取地址没有意义。
------解决方案--------------------
2.3 Assign an address to a Function Pointer
It 's quite easy to assign the address of a function to a function pointer. You simply take the name of a suitable and known function or member function. Although it 's optional for most compilers you should use the address operator & infront of the function 's name in order to write portable code. You may have got to use the complete name of the member function including class-name and scope-operator (::). Also you have got to ensure, that you are allowed to access the function right in scope where your assignment stands.
// 2.3 assign an address to the function pointer
// Note: Although you may ommit the address operator on most compilers
// you should always use the correct way in order to write portable code.
// C
int DoIt (float a, char b, char c){ printf( "DoIt\n "); return a+b+c; }
int DoMore(float a, char b, char c)const{ printf( "DoMore\n "); return a-b+c; }
pt2Function = DoIt; // short form
pt2Function = &DoMore; // correct assignment using address operator