程序:
#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
4 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 。
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
4 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 。