程序:
#include <stdio.h>
void func()
{
int i = 1;
}
int main()
{
printf(“%d\n”,main);
printf(“%d\n”,*main);
printf(“%d\n”,**main);
printf(“%d\n”,***main);
printf(“%d\n”,func);
printf(“%d\n”,*func);
printf(“%d\n”,**func);
printf(“%d\n”,***func);
return 0;
}
结果:
eagle@eagle-QJC4:~$ ./a.out
4195652
4195652
4195652
4195652
4195638
4195638
4195638
4195638
#include <stdio.h>
void func()
{
int i = 1;
}
int main()
{
printf(“%d\n”,main);
printf(“%d\n”,*main);
printf(“%d\n”,**main);
printf(“%d\n”,***main);
printf(“%d\n”,func);
printf(“%d\n”,*func);
printf(“%d\n”,**func);
printf(“%d\n”,***func);
return 0;
}
结果:
eagle@eagle-QJC4:~$ ./a.out
4195652
4195652
4195652
4195652
4195638
4195638
4195638
4195638
解决方案:40分
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 。