返回信息流第一问:选择题:给定函数f的参数和返回值的定义:
int f() {
// 内容略
}
下面4个语句,有一个会造成编译错误。是哪个?
// A
(* * * * * * * * * * * * * * * * * * * * * * * * * f)();
// B
(* * & * & * & * & * & * & * & * & * & * & * & * & f)();
// C
(* * & & * * & & * * & & * * & & * * & & * * & & * f)();
// D
(& * * * * & * * * * & * * * * * * * * & * * * & * f)();
p.s. 稍微改了一下,每个之间加了一个空格,用来反映我的本意。但答案没变。
第二问:选择题:上一问中那3个可以编译通过的程序,结果如何?
A、效果和直接调用f()一样,而且任何C编译器下编译都是这样。
B、编译通过,好像可以正确执行,但换一个编译器也许就不行了,这是“什么都可以发生,从什么都不发生到机器冒烟都可能”的程序。
C、程序一定会崩掉。
D、在Linux下一定是和直接调用f()一样,在Windows下程序会崩溃。
E、取决于具体的f(),对于某些f碰巧可以调用,但另一些f会不正常。
这是一条镜像帖。来源:北邮人论坛 / cpp / #88807同步于 2015/9/25
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[C语言也惊喜]智力问答:C函数指针
nuanyangyang
2015/9/25镜像同步35 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
【 在 gdl 的大作中提到: 】
: 编译器告诉我是C,不过没看懂,暖神的问题都好神奇!!
我改了一下代码,现在编译器应该报不同的错,但答案一样。现在报的应该是我的本意(原来编译器应该会说&&是“与”运算,但我是希望它是两个取地址运算符)。
是不是因为这个,变成了 rvalue,于是就不符合语法了
A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, the _Alignof operator,65) 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’’.
【 在 glazard 的大作中提到: 】
: 是不是因为这个,变成了 rvalue,于是就不符合语法了
: A function designator is an expression that has function type. Except when it is the operand of the sizeof operator, the _Alignof operator,65) 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’’.
嗯。是这么回事。