LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   passing function pointer as argument (https://www.linuxquestions.org/questions/programming-9/passing-function-pointer-as-argument-212871/)

worldmagic 08-03-2004 07:18 AM

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*)) {
    ...
    if( comp(... , ...) ) { ...
    ...
    }
}

int main() {
 
  myFunc( strcmp );

  return EXIT_SUCCESS;
}

Its the call to myFunc in main that sais incompatible type.. strcmp are defined as:
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:


int main() {

  myFunc( int(*)(void*, void*) strcmp);
...
}


worldmagic 08-03-2004 07:20 AM

Ofcourse it shouldn't read "myFunc*(" but "myFunc(" in the abow example, question remains about the type issuses.

Ephraim 08-03-2004 10:05 AM

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*);

void myFunc(COMP_PROC comp) {

Ciao Ephraim

jim mcnamara 08-03-2004 10:06 AM

Here is one way to do it -
Code:

#include <string.h>
#include <stdlib.h>

void myFunc(int (*comp)()) {
    char *a="123";
    char *b="234";
    if( comp(a,b)!=0 ) {
        printf("hi there\n");   
    }
}

int main() {
 
  myFunc( strcmp );

  return EXIT_SUCCESS;
}


osvaldomarques 08-03-2004 11:17 PM

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);

worldmagic 08-04-2004 08:47 AM

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>
#include <stdlib.h>

void joy(int (*debug)()) {
        debug("JOY\n");
}

int main() {

        joy(printf);

        return EXIT_SUCCESS;
}

But I got the following error while compiling:

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?

supinlick 08-04-2004 01:06 PM

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

osvaldomarques 08-04-2004 03:33 PM

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) {
  return(strcmp((const char *) a, (const char *) b));
}



All times are GMT -5. The time now is 08:12 PM.