In the function doubleadd i want the result as the summation of x and function add
#include <stdio.h>
int add(int a,int b)
{ return (a+b); }
int doublea开发者_开发百科dd(int x,int y=(*add)(int a,int b))
{ return (x+y); }
void main()
{
void (*ptr)(int,int);
ptr=add;
int y=ptr(5,7);
printf("%d",y);
y=doubleadd(3,ptr(5,7));
}
please help me with this problem
You don't need to do all that mess! This will be fine:
y = doubleadd(3, add(5,7));
and the prototype of doubleadd is
int doubleadd(int x, int y) { ... }
you can pass complex expressions as parameters, too
In the unlikely event you want to turn your C code into some bastard child of a functional language:
#include <stdio.h>
typedef int (*addfun)( int a, int b );
int add(int a,int b) {
return a + b;
}
int doubleadd(int x, addfun f, int a, int b ) {
return x + f( a, b );
}
int main() {
addfun fn = add;
int y = doubleadd(3, fn, 5, 7 );
printf( "%d\n", y );
}
精彩评论