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

Time:01-09

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?

CodePudding user response:

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.

CodePudding user response:

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.

  •  Tags:  
  • Related