yes. a pointer to a function returning an int and taking two void pointers as parameters.
yes. a pointer to a function returning an int and taking two void pointers as parameters.
so can have a type that returns something and takes something as the parameter? this seems weird to me..
no, the type is a pointer. the type that the pointer points to is a function. that function is a type that returns an int and takes two void *s as parameters.Code:#include <stdio.h> typedef int (*COMPAREFUNC)(void*, void*); /* ^ * |Notice the similarity in these two signatures. * |Same return type, same type and number of arguments. * v */ int func(void*param1,void*param2); int main() { COMPAREFUNC my_func_pointer; char a=1,b=0,c=0,d=0; func(&a,&b); /* prints "in func" */ my_func_pointer = func; /* my_func_pointer now points to func */ my_func_pointer(&c,&d); /* prints "in func" */ return 0; } int func(void *param1, void *param2) { puts("in func"); return 0; }
Last edited by robwhit; 04-28-2008 at 06:49 PM. Reason: add code
okay so we can do a typedef for such thing? not only for int, float, etc.. but also something that takes a parameter and returns something?
well, you can do it for a /pointer/ to a function (or a pointer to a pointer to a function or...), but I don't think you can do it for a function.
see my edit in my last message for an example.
yeah I understand your example... but yet still I am not still sure somehow
Wow, talk about complicated stuff. I'll save this thread to my hard drive. I never know when I could use answers to an extremely intelligent question like this one
The typedef simply means you can write this
COMPAREFUNC my_func_pointer;
As opposed to this
int (*my_func_pointer)(void*, void*);
One you can live with, but the syntax is heavy on the parentheses and verbose with all the parameter types, which can be hard on the fingers if you need lots of them. The typedef saves you from all that.
Since no one has mentioned it yet, http://www.newty.de/fpt/index.html
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
maybe I am just not used to the typedef that can take parameters..that's all my confusion is
The typedef doesn't take parameters. However, the type itself is a function pointer, and part of a function pointer is the arguments to the function. Yes, it takes a bit to get used to. If I haven't used function pointers for a bit, I have to look up and/or think about where all the parenthesis and stars belong.
Here's one that takes a bit of thinking.
It does get a bit simpler if you do two typedefs:Code:typedef int *(*funcptr)(int (*func1)(void));
That's a function pointer (called funcptr) that returns a pointer to int, and takes a function pointer that returns int, which takes no arguments.Code:typedef int (*subfunc)(void); typedef int *(*funcptr)(subfunc func1);
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Function pointers are obscure, but when you're used to them then you'll see the power :-)
Is and array of function pointers where functions return unsigned char and have an argument which is function pointer which returns void and has void as an argument.Code:static unsigned char (*const GpsHandler[NUM_FUNS][NUM_TPS])(void (*GpsMsgs)(void)) = {...};
To add another thing into the mix. I usually do:
typedef int (COMPAREFUNC)(void*, void*);
(As opposed to
typedef int (*COMPAREFUNC)(void*, void*);
Note the missing *)
So that when I define a function pointer, I can do:
COMPAREFUNC* pFnc;
It's now easier to see it's actually a pointer.
Now that it's bad to put the * in the type, but I prefer the second way. Either one is possible and both just as good.
thanks guys for helping me.. can you give me some other typedef examples of a function pointer
Typical Windows API:
Will print 1 - 100 (or should). The callback function can cancel by returning false instead of true.Code:typedef int bool; #define true 1 #define false 0 bool callback(int id) { printf("%i\n"); return true; } typedef bool (callbackptr)(int); void example(callbackptr* pcallback) { int i = 0; for (; i < 100; i++) { if (! pcallback(i) ) /* If function returns false, then break the loop */ break; } } int main(void) { example(&callback); return 0; }