seaneff
seaneff

Reputation: 13

int (*d)(int *) = foo, what does this part of the code do?

int foo (int x) {
    int (*d)(int *) = foo;  //what is the meaning of this line?
    ...
}

this is an old practice question from my school, but i couldn't find the solution for it. Is it initializing a variable to a function?

Upvotes: 1

Views: 292

Answers (2)

Bunny
Bunny

Reputation: 342

As far as I can tell the a declaration is irrelevant to your question. int (*d)(int *) defines a pointer d to a function that takes an int * and returns an int. = foo sets d equal to the address of foo.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

This record

int (*d)(int *) = foo; 

is a declaration of the function pointer d to function that has the return type int and one parameter of the type int *. This pointer is initialized by the address of the function foo (the function designator is implicitly converted to pointer to it).

Pay attention to that either the function foo should be declared like

int foo (int *x) {

or the pointer should be declared like

int (*d)(int ) = foo; 

Otherwise in this declaration

int (*d)(int *) = foo; 

there are used incompatible pointer types.

Upvotes: 3

Related Questions