passing function pointer as argument
Hello.
I get "incompatible type" when trying to do the following .. how should I write my code? Code:
void myFunc*(int (*comp)(void*, void*)) { int strcmp(const char*, const char*) I can understand that 'const' might make my code incompatible, but why do 'char' ? And how should I write my code for it to work? I do not want to do this to make it work: Code:
|
Ofcourse it shouldn't read "myFunc*(" but "myFunc(" in the abow example, question remains about the type issuses.
|
wyh you don't simple declare your function like this:
Code:
void myFunc(int (*comp)(char*, char*)) { by the way i think it is not so confusing if you do it like this: Code:
typedef int (*COMP_PROC)(char*, char*); |
Here is one way to do it -
Code:
#include <string.h> |
Hi worldmagic,
The reason of "incompatible clause" is the declaration of the function you use as parameter. The strcmp receives 2 "const char *" and you declared your prototype as 2 "void *". From the man pages, Code:
int strcmp(const char *s1, const char *s2); |
Many thanks to all of you.
I got a reason for defining it to void* instead of char*, I should have included that in the question. My fault =) The reason is that my library can take diffrent types of objects, and therefor every other function I use is defined as taking void*. And thats also why I need the user to provide me with the comparator function (the lib dont know how to compare these 'void*' objects). I have had problems with typedefing functions in the passed, thats why I didnt use it. Worth an other try.. To create a function pointer without arguments was a smart move, thats actually the same as providing it with a void argument.. but, does it work on all compilers? seems abit hazardous to do, as the user can send use _any_ function ;-).. But hey they could code a segfault in there function too.. I will use this, many thanks!! ... Mm, created some testcode: Code:
#include <stdio.h> test.c: In function `main': test.c:10: warning: passing arg 1 of `joy' from incompatible pointer type .. Is there any other way to solve this problem without getting a warning from the compiler? If nothing else helps, maybe I have to ask the user to define a macro that I then use in a typedef in my headerfile and let it do some magic to get around this.. but I dont think thats an nice solution? |
Hello -
I suggest using the typedef solution, since it's not only compiler-safe, but explicitly states what you expect from the 'user'. You should let the compiler do all the work it can do for you... Cheers, Sean |
Hi worldmagic,
When you declare a function without parameters, you are not prototyping it. It is the K&R style. You run into the risk of discover the error only when running the program. You may have an erratic behavior. The best is to prototype it and always create your functions in accordance to the prototype. You resolve your problems at the compile time. If someone needs only an strcmp, it can embed it into a kind of generic function, for example, Code:
int generic_compare(void *a, void *b) { |
All times are GMT -5. The time now is 08:12 PM. |