Function pointer

Hi could you show me how to compare x with the return from the int (*poly)( int a)
Thanks


 
int evaluate( int x, int (*poly)( int a) )
The syntax to calling a function via a function pointer is no different to calling a function directly, so getting a return value is no different either; comparing a value returned from a function is also the exactly same.
Thank you for the help,but can somebody show with code ,how is done
1
2
3
4
5
6
7
8
9
10
11
12
13
int polyF(int i)
{
    return i;
}
int evaluate( int x, int (*poly)( int a) )
{
    poly=polyF;
    if(x>poly(a))
    return x;
    else
    return poly(a);
}
closed account (zb0S216C)
Lio wrote:
but can somebody show with code ,how is done

Are you asking how it works? If so, here's a brief explanation:

A function pointer is just a pointer that points to the first instruction of the pointed-to function. When the pointer is invoked, the pointed-to function is given the arguments; the arguments are fully evaluated, and then the function executes. A function-pointer requires that the pointed-to function matches the declaration of the function-pointer exactly.

Wazzak
Thanks,I am asking somebody to corect my code
How can I compare the argument x with the int return from the pointed function
Last edited on
1
2
3
4
5
int evaluate( int x, int (*poly)(int) )
{
    int y = poly(x) ;
    return x>y ? x : y ;
}

poly expects 1 argument. You are passing 'a', that is never defined.
Maybe it should be
1
2
3
4
5
6
7
typedef int (*function)(int);
int evaluate(int x, function f, int value){
  return std::max( x, f(value) );
}

//call it as
evaluate(0, polyF, 42);
Topic archived. No new replies allowed.