打印函数名,函数名前加n个*为什么结果相同?


程序:
#include <stdio.h>
void func()
{
int i = 1;
}
int main()
{
printf("%p\n",main);
printf("%p\n",*main);
printf("%p\n",**main);
printf("%p\n",***main);
printf("%p\n",func);
printf("%p\n",*func);
printf("%p\n",**func);
printf("%p\n",***func);
return 0;
}
结果:
eagle@eagle-QJC4:~$ ./a.out 
4195652
4195652
4195652
4195652
4195638
4195638
4195638
4195638
以下答案来自http://blog.chinaunix.net/uid-10314004-id-2964074.html
C语言中的函数是一种function-to-pointer方式,即对于函数,要将其转化成指针类型的.
ptr是函数指针类型,则*ptr是该指针所指向的函数,而由于函数自身又转化成指针了,所以说*ptr又转化成了ptr这种函数指针类型,因此,前面无论有多少个*号,最后都是对这个函数的调用.

下面的答案来自   @fefe82 的回答 @fefe82
C11 draft n1570
6.3.2.1 Lvalues, arrays, and function designators
4 A function designator is an expression that has function type. Except when it is the
operand of the sizeof operator, the _Alignof operator, 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’’
.

6.5.3.2 Address and indirection operators
The unary * operator denotes indirection. If the operand points to a function, the result is
a function designator; if it points to an object, the result is an lvalue designating the
object.
 If the operand has type ‘‘pointer to type’’, the result has type ‘‘type’’. If an
invalid value has been assigned to the pointer, the behavior of the unary * operator is
undefined.

function designator 会自动转换成 pointer to function, 加一个 * 之后变成 function designator ,然后又自动转换成 pointer to function,周而复始,无论你加多少 * ,最后拿到的还是一个 pointer to function 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值