以下哪个函数调用是有效的? [英] Which of the following function invocations is valid?

查看:108
本文介绍了以下哪个函数调用是有效的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑声明

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

int second(char,float);

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

first(* second);



B) first(& second);



first(second) ;



D)以上皆非

任何人都可以解释我这个代码?

解决方案

所有三个调用都是有效的。

到C标准(6.3.2.1左值,数组和函数指示符)


4函数指示符是一个具有函数类型的表达式。
除了当它是sizeof操作符65的操作数时)或者一元&
运算符,函数指示符的类型为''函数返回类型''
被转换为一个表达式,其类型''指向函数
返回类型''


此外,您甚至可以写入

  first(*** ***秒); 

这是一个在表达式中使用的函数标识符,它被隐式转换为函数本身的指针,除了事实上一种情况是当它被用作&

这是一个演示程序

  #include< stdio.h> 

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


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

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

返回0;

$ / code>

它的输出是

 您好! 

考虑到函数 first 也可以被声明为像

  char first(int(char,float)); 

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


Consider the declarations

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

int second(char, float);

Which of the following function invocations is valid?

A) first (*second);

B) first (&second);

C) first (second);

D) none of the above

Can any one please explain me this code?

解决方案

All three calls are valid.

According to the C Standard (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 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’’

Moreover you can even write

first( ******second );

That is a function designator used in expressions is implicitly converted to pointer to the function itself except of in fact one case when it is used as an operand of the & operator where the address of the function is taking explicitly.

Here is a demonstrative program

#include <stdio.h>

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


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

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

    return 0;
}

Its output is

Hello!

Take into account that the function first also could be declared like

char first (int (char, float)) ;

A function parameter having a function type is implicitly adjusted to pointer to function.

这篇关于以下哪个函数调用是有效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆