void to pointers

// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}

int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;

m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}



I didn't get int operation (int x, int y, int (*functocall)(int,int))
part of this program , Can anyone explain this and give example of a program which uses only int operation (int x, int y, int (*functocall)(int,int)).
I will be thankful.
int (*functocall)(int,int) declares 'functocall' as a pointer to a function taking two ints as arguments and returning an int.
The program you posted is meant to explain that
Last edited on
yes ,But this program has a lot of other things too.
I want to make aprogram which uses only int (*functocall)(int,int)
no other functions.
Last edited on
int (*functocall)(int,int) is a function pointer, not a real function. In order to use a function pointer you must have other functions for it to point to, otherwise it's useless.

1
2
3
4
5
6
// makes the function pointer 'minus' point to the function 'subtraction'
int (*minus)(int,int) = subtraction;

// now the following two lines of code call the same function:
m = (*minus)(5,3);
m = subtraction(5,3);


Those last two lines both call the same function ('subtraction').
You must have at least a function to use a pointer to function, try to see if this example is easier to understand:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int function( int a, int b )//sample function
{
    return a+b;
}

int main()
{
    int (*functocall)(int,int) = function;//declare 'functocall' and set it to the position of 'function'
    
    std::cout << functocall ( 1, 2 );//call the function pointer with some arguments and display the result

    return 0;
}
Thank you bazzy .this example is easier.
Topic archived. No new replies allowed.