gpt4 book ai didi

c - 以下哪个函数调用是有效的?

转载 作者:行者123 更新时间:2023-11-30 20:04:16 24 4
gpt4 key购买 nike

考虑声明

char first (int (*) (char, float)) ;

int second(char, float);

以下哪个函数调用是有效的?

A) first (*second);

B) first (&second);

C) first (second);

D)以上都不是

谁能给我解释一下这段代码吗?

最佳答案

所有三个调用均有效。

根据 C 标准(6.3.2.1 左值、数组和函数指示符)

4 A function designator is an expression that has function type. Except when it is the operand of the sizeof operator65) 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’’

此外,你甚至可以写

first( ******second );

即表达式中使用的函数指示符被隐式转换为指向函数本身的指针,除了实际上将其用作 & 运算符的操作数(其中函数的地址显式获取)的情况。

这是一个演示程序

#include <stdio.h>

void g( void ( *f )( void ))
{
f();
}


void f( void )
{
puts( "Hello!" );
}

int main( void )
{
g( **********f );

return 0;
}

它的输出是

Hello!

考虑到函数first也可以这样声明

char first (int (char, float)) ;

具有函数类型的函数参数被隐式调整为指向函数的指针。

关于c - 以下哪个函数调用是有效的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com