If a make a function and one of it's params takes a function pointer, how do I call the function that is passed as an argument?
Printable View
If a make a function and one of it's params takes a function pointer, how do I call the function that is passed as an argument?
How do you mean? you can just place the function call of one function inside that of another as long as it returns the same value as the parameter of the first.
Code:int addnum(int a, int b)
{
return(a+b);
}
printf("%d", addnum(10, 5));
Oh nice, preview my message, then hit back to edit and it's gone..
let's try again :(
I want to do the implementation of a function that calls the callback function.
RETURN_TYPE FunctionName(some params, function pointer parameter)
{
do some stuff;
call the function using it's passed function pointer;//this is what I can't find an example of
return whatever;
}
this code checks if two strings are the same... basically it's a wrapper for cmpstr...
any ?uestions?Code:#include <stdio.h>
#include <string.h>
void check (char *a, char *b, int (*cmp) ()); // how to declare the function to accept the function pointer
int main (void)
{
char s1[80], s2[80];
int (*p)(); // function pointer
p = strcmp;
gets (s1);
gets (s2);
check (s1, s2, p);
}
void check (char *a, char *b, int (*cmp) ())
{
printf ("testing for equality\n");
if (
!(*cmp) (a, b) // this is how you call the function via it's pointer...
)
printf ("equal");
else
printf ("not equal");
}
On most compilers you should be able to call it like a standard function - cmp(a,b);
Thanks to both of you
but you need to dereference it... no?
No need to dereference it ;)
int f( int x, FUNCTION_POINTER p )
{
return p( x );
}
WHAT! you must be trippin' nv! but... but.... why?!?!?!?!
Because having function values is impossible.
well that's true, but it's the nature of pointers tho... since they contain addresses [in these cases a code address]... and, actually, couldn't you change the function pointer's pointing address manually?
Since we're on the topic. When you call a function, any memory used by ints bools or other variables that are declared in the function is freed up once the function returns a value, correct?
How is a pointer or a class any different in that it needs to be dereferenced or decunstructed?