Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading