We can declare not just variables but functions
or pointer to functions also
in the main function like this:

double my_f(double);
double (*my_fp)(double);

But what can we do with this?
For example how can we give a value for my_f or my_fp?
I try lots of variants with * and & but can not handle the problem.
my_f = *my_previuosly_defined_single_variable_double_function
does not work.

Recommended Answers

/*original function*/
void my_fun(int);

/*pointer to function*/
void (*funPtr)(int);

/*assigning the function to the pointer*/
funPtr = my_fun;

/*calling the function using pointer*/

checkout the link

Jump to Post

All 4 Replies

Thanks the answer, but:

int main() 
   double fv(double);
   double (*fp)(double);
   fp = fv; 

does not work for me, I use MinGW Compiler:
undefined reference to `fv(double)'

it is also strange for me: fp = fv;
because they are different kind of type

but if it is OK for another compiler than
how can I give a value to fv and not to the pointer:
fv = ... ?

undefined reference to `fv(double)'

its because u haven't defined the function

double fv(double);

In line #3 of your code, its just a declaration which says that there exists some function as such. Its not definition. Define your fuinction and see.

it is also strange for me: fp = fv;
because they are different kind of type

read about function pointers in detail.

Functions generally are declared as prototypes above the main().

double fv(double);
int main()

The function definition (i.e. text of the function) must be written outside of the main(). Finally, in your case the function should be called within main().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.