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?
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?
WebSnozz-
Cats have no butt cheeks.
If one farted, then it would make a flute noise.
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));
All spelling mistakes, syntatical errors and stupid comments are intentional.
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;
}
WebSnozz-
Cats have no butt cheeks.
If one farted, then it would make a flute noise.
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"); }
hasafraggin shizigishin oppashigger...
On most compilers you should be able to call it like a standard function - cmp(a,b);
zen
but you need to dereference it... no?
hasafraggin shizigishin oppashigger...
No need to dereference it
int f( int x, FUNCTION_POINTER p )
{
return p( x );
}
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
WHAT! you must be trippin' nv! but... but.... why?!?!?!?!
hasafraggin shizigishin oppashigger...
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?
hasafraggin shizigishin oppashigger...
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?