函数指针的 “*” 和 “&”

Loongsun Chan 2015-02-12 03:55:20
看到一些函数指针的用法很奇怪,到底用不用“*” 和 “&”呢?于是在网上函数指针资料的时候,在网上看到了这样的说法:



还有


...全文
706 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_22255215 2015-02-15
  • 打赏
  • 举报
回复
wanggou ji能感动对方感受到
paschen 版主 2015-02-15
  • 打赏
  • 举报
回复
引用 2 楼 openXMPP 的回复:
修饰函数指针时,加不加"&" 函数指针是一样的 #include <stdio.h> void foo(){ printf("foo called\n"); } int main(){ printf("%p\n", foo); printf("%p\n", &foo); void (*FUNC) (); FUNC = foo; FUNC(); printf("address %p\n", FUNC); printf("address %p\n", &FUNC); return 0; } 输出 macbook:test mac$ ./a.out 0x10d99deb0 0x10d99deb0 foo called address 0x10d99deb0 address 0x7fff52262a70 注意看最后一个输出
学习了!
勇敢小鱼 2015-02-12
  • 打赏
  • 举报
回复
你定义typedef int func(int, int) 使用时要这样 func * ptrFun 这样才定义了一个指针 若定义typedef int (func *)(int, int) 使用时 func ptrFun 直接定义指针
Loongsun Chan 2015-02-12
  • 打赏
  • 举报
回复
引用 5 楼 dbzhang800 的回复:
你需要明白,你这儿涉及到两个类型。 比如: typedef int func(int, int); typedef int (*func_ptr)(int, int); func 和 func_ptr 是两种不同的类型,你可以自己输出结果看看。 std::cout<<typeid(func).name()<<std::endl; std::cout<<typeid(func_ptr).name()<<std::endl; 某些情况下,前者可以隐式转换成后者。 另外:你举得例子 func x = add; 在 gcc 下无法编译通过。
func x = add; 我在vc里面就无法编译通过了是怎么回事呢?
Loongsun Chan 2015-02-12
  • 打赏
  • 举报
回复
引用 7 楼 mujiok2003 的回复:
引用
A function designator is an expression that has function type. Except when it is the operand of the sizeof operator54) or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.
C99 6.3.2.1.4
专业啊 我按照你的引用搜索了一下 说是除了个别特例之外 函数名字会被转化为指向自己的指针? “that has function type”和“ with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’ 怎么理解呢?
Loongsun Chan 2015-02-12
  • 打赏
  • 举报
回复
引用 9 楼 openXMPP 的回复:
[quote=引用 6 楼 nsxzclx 的回复:] [quote=引用 2 楼 openXMPP 的回复:] 修饰函数指针时,加不加"&" 函数指针是一样的 #include <stdio.h> void foo(){ printf("foo called\n"); } int main(){ printf("%p\n", foo); printf("%p\n", &foo); void (*FUNC) (); FUNC = foo; FUNC(); printf("address %p\n", FUNC); printf("address %p\n", &FUNC); return 0; } 输出 macbook:test mac$ ./a.out 0x10d99deb0 0x10d99deb0 foo called address 0x10d99deb0 address 0x7fff52262a70 注意看最后一个输出
我试了你的程序,确实是这样的,可是我感性理解就觉得很奇怪。[/quote] 没啥好奇怪的 C++作为一个"名声"不太好的语言 的确是这样的 你看到的 跟你直观认识 往往有些差距[/quote] 那就没办法理解了 只能记住了是吗?
乔巴好萌 2015-02-12
  • 打赏
  • 举报
回复
引用 6 楼 nsxzclx 的回复:
[quote=引用 2 楼 openXMPP 的回复:] 修饰函数指针时,加不加"&" 函数指针是一样的 #include <stdio.h> void foo(){ printf("foo called\n"); } int main(){ printf("%p\n", foo); printf("%p\n", &foo); void (*FUNC) (); FUNC = foo; FUNC(); printf("address %p\n", FUNC); printf("address %p\n", &FUNC); return 0; } 输出 macbook:test mac$ ./a.out 0x10d99deb0 0x10d99deb0 foo called address 0x10d99deb0 address 0x7fff52262a70 注意看最后一个输出
我试了你的程序,确实是这样的,可是我感性理解就觉得很奇怪。[/quote] 没啥好奇怪的 C++作为一个"名声"不太好的语言 的确是这样的 你看到的 跟你直观认识 往往有些差距
Loongsun Chan 2015-02-12
  • 打赏
  • 举报
回复
引用 5 楼 dbzhang800 的回复:
你需要明白,你这儿涉及到两个类型。 比如: typedef int func(int, int); typedef int (*func_ptr)(int, int); func 和 func_ptr 是两种不同的类型,你可以自己输出结果看看。 std::cout<<typeid(func).name()<<std::endl; std::cout<<typeid(func_ptr).name()<<std::endl; 某些情况下,前者可以隐式转换成后者。 另外:你举得例子 func x = add; 在 gcc 下无法编译通过。
输出了 int __cdecl(int,int) int (__cdecl*)(int,int) 关于__cdecl我只知道是调用约定……眼拙没看清,typedef int func(int, int);这种形式的我之前都没见过啊~新问题又来了~
mujiok2003 2015-02-12
  • 打赏
  • 举报
回复
引用
A function designator is an expression that has function type. Except when it is the operand of the sizeof operator54) or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.
C99 6.3.2.1.4
Loongsun Chan 2015-02-12
  • 打赏
  • 举报
回复
引用 2 楼 openXMPP 的回复:
修饰函数指针时,加不加"&" 函数指针是一样的 #include <stdio.h> void foo(){ printf("foo called\n"); } int main(){ printf("%p\n", foo); printf("%p\n", &foo); void (*FUNC) (); FUNC = foo; FUNC(); printf("address %p\n", FUNC); printf("address %p\n", &FUNC); return 0; } 输出 macbook:test mac$ ./a.out 0x10d99deb0 0x10d99deb0 foo called address 0x10d99deb0 address 0x7fff52262a70 注意看最后一个输出
我试了你的程序,确实是这样的,可是我感性理解就觉得很奇怪。
dbzhang800 2015-02-12
  • 打赏
  • 举报
回复
你需要明白,你这儿涉及到两个类型。 比如: typedef int func(int, int); typedef int (*func_ptr)(int, int); func 和 func_ptr 是两种不同的类型,你可以自己输出结果看看。 std::cout<<typeid(func).name()<<std::endl; std::cout<<typeid(func_ptr).name()<<std::endl; 某些情况下,前者可以隐式转换成后者。 另外:你举得例子 func x = add; 在 gcc 下无法编译通过。
乔巴好萌 2015-02-12
  • 打赏
  • 举报
回复
引用 3 楼 nsxzclx 的回复:
[quote=引用 1 楼 wangjunsheng 的回复:] 可以不用 用了表明函数指针更直观些,当然这个取决于编程风格吧
嗯嗯 是的 只是看着别扭 这是C语言还是编译器决定的呢?[/quote] C specification规定的
Loongsun Chan 2015-02-12
  • 打赏
  • 举报
回复
引用 1 楼 wangjunsheng 的回复:
可以不用 用了表明函数指针更直观些,当然这个取决于编程风格吧
嗯嗯 是的 只是看着别扭 这是C语言还是编译器决定的呢?
乔巴好萌 2015-02-12
  • 打赏
  • 举报
回复
修饰函数指针时,加不加"&" 函数指针是一样的 #include <stdio.h> void foo(){ printf("foo called\n"); } int main(){ printf("%p\n", foo); printf("%p\n", &foo); void (*FUNC) (); FUNC = foo; FUNC(); printf("address %p\n", FUNC); printf("address %p\n", &FUNC); return 0; } 输出 macbook:test mac$ ./a.out 0x10d99deb0 0x10d99deb0 foo called address 0x10d99deb0 address 0x7fff52262a70 注意看最后一个输出
乃不知有汉 2015-02-12
  • 打赏
  • 举报
回复
可以不用 用了表明函数指针更直观些,当然这个取决于编程风格吧

65,180

社区成员

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

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