Which of the following function invocations is valid? [closed]

余生颓废 提交于 2019-12-13 10:46:00

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/42165003/which-of-the-following-function-invocations-is-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!