"minus" is the function pointer pointing to function called "subtraction" which take 2 integers and return 1 integer.
Inside the main() function, there is this sentence - "n = operation (20, m, minus);" and operation function is defined as below:
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}
I understand that "minus" is the function pointer and it has an address of subtraction function. When operation function is called in main() function, and pass 2 integers value and minus to operation function as input arguments, I would like to know
1. where does "functiontocall" pointing to?
2. Is it correct to say that passing the address of subtraction() function to another function pointer called "functiontocall" when we write "n = operation (20, m, minus);" ?
functocall is actually a datatype now and so it doesn't point to anything, instances of functocall can be made to point to functions with matching signatures and return values. functocall has been typedef'd within below program to make this point clearer:
Is it correct to say that passing the address of subtraction() function to another function pointer called "functiontocall" when we write "n = operation (20, m, minus);"
again same confusion b/w type and instance of a type, here we can only assign address of subtraction to an instance of functocall like minus, etc. in the below program I've added in another function with similar signature and return value, addition() and it shows how any functions of the same 'type' can be assigned to the instance of functocall:
> int operation ( int x, int y, int (*functocall)(int,int) ) ;
operation is a function which can be called with three arguments;
The first parameter int x is of type int, the function gets a copy of the the first integer argument.
The second parameter int y is of type int, the function gets a copy of the the second integer argument.
The third parameter has the name functocall, the function gets a copy of the the third argument, a pointer to a function of the appropriate type.
Note that just as x is the name of the variable (the parameter) and its type is int, functocall is the name of the variable; its type is int (*)(int,int) ie. 'pointer to function which take two arguments of type int and returns a value of type int'
In main()operation (20, m, minus); is a call to the function; when evaluated, the formal parameters x and y are initialised with 20 and m; functocall is initialised to point to the function minus
In the function (*functocall)(x,y); is evaluated as call function pointed to by functocall passing x and y as the two arguments. With the invocation operation (20, m, minus); in main(), (*functocall)(x,y); calls the function minus with 20 and m as the two arguments.
Note that each time the function operation is called, the objects x, y and functocall are intialised with the three arguments in the call; the lifetime of these objects end when the function returns. The next time the function is called, three fresh objects x, y and functocall are created, initialised with the three arguments in the second call.