-1

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?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
user3639779
  • 79
  • 1
  • 9

1 Answers1

3

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335