LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-03-2004, 07:18 AM   #1
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
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);
...
}
 
Old 08-03-2004, 07:20 AM   #2
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Original Poster
Rep: Reputation: 15
Ofcourse it shouldn't read "myFunc*(" but "myFunc(" in the abow example, question remains about the type issuses.
 
Old 08-03-2004, 10:05 AM   #3
Ephraim
LQ Newbie
 
Registered: May 2002
Posts: 11

Rep: Reputation: 0
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
 
Old 08-03-2004, 10:06 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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;
}
 
Old 08-03-2004, 11:17 PM   #5
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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);
 
Old 08-04-2004, 08:47 AM   #6
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Original Poster
Rep: Reputation: 15
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?
 
Old 08-04-2004, 01:06 PM   #7
supinlick
LQ Newbie
 
Registered: Aug 2004
Location: virginny
Posts: 11

Rep: Reputation: 0
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
 
Old 08-04-2004, 03:33 PM   #8
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
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));
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segfault when passing array[] as argument froboozle Programming 11 06-28-2005 03:06 PM
Function Pointer as an Argument in C trutnev Programming 5 05-24-2005 10:22 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
How does passing argument to main work Linh Programming 5 06-27-2003 01:08 PM
PHP Script argument passing error... lokee Linux - Software 5 04-24-2003 09:42 AM


All times are GMT -5. The time now is 09:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration