Find the answer to your Linux question:
Results 1 to 3 of 3
I have a program which was coded by mix of c++ and c code, when I test the function pointer in C code, it is ok, but when I try ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    19

    question about function pointer compile error


    I have a program which was coded by mix of c++ and c code, when I test the function pointer in C code, it is ok, but when I try to merge it with C++ code, it give me compile error like this:

    gcc -g -Wall -D_GNU_SOURCE -DDEBUG -c wmain.cpp
    wmain.cpp:42: invalid conversion from `void*(*)(void*)' to `void (*)(void*)'
    wmain.cpp:42: invalid conversion from `void*(*)(void*)' to `void (*)(void*)'
    wmain.cpp:42: invalid conversion from `void*(*)(void*)' to `void (*)(void*)'
    wmain.cpp: In function `int main(int, char**)':
    wmain.cpp:97: invalid conversion from `void (*)(void*)' to `void*(*)(void*)'
    gmake: *** [wmain.o] Error 1

    Here it is the snapshot of my two files (one is the header file to define the function pointer)

    In threadhandler.h

    #ifdef __cplusplus
    extern "C" {
    #endif

    ......

    #define MSG1 1
    #define MSG2 2
    #define MSG3 3


    extern void *PrintHello1(void *params);
    extern void *PrintHello2(void *params);
    extern void *PrintHello3(void *params);

    typedef void (*Func)(void *);

    struct PrintHi {
    int msg;
    Func funcptr;
    };

    #ifdef __cplusplus
    }
    #endif

    In wmain.cpp

    ......

    struct PrintHi PrintHiTable[3] = {
    { MSG1, PrintHello1 },
    { MSG2, PrintHello2 },
    { MSG3, PrintHello3 }
    }; // here it is the LINE 42

    int main(int argc, char *argv[])
    {

    ......

    for (i=0; i< NUM_THREADS; i++) {

    ......

    pthread_create(&threads[i], NULL, PrintHiTable[i].funcptr, (void *) &thread_data_array[i]); // here it is the LINE 97

    }

    }

    Please let me know if you have any good solution.
    Thanks.

  2. #2
    Super Moderator Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,809
    Shouldn't you be using:

    Code:
    typedef void Func&#40;void *&#41;;
    
    struct PrintHi &#123; 
    int msg; 
    Func* funcptr; 
    &#125;;
    Unless your functions really do return a void*, in which case you should set your typedef to 'void* Func(void*);'
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    Linux Newbie
    Join Date
    Nov 2004
    Location
    New York
    Posts
    150
    Code:
     invalid conversion from `void*&#40;*&#41;&#40;void*&#41;' to `void &#40;*&#41;&#40;void*&#41;'
    In C you can convert implicitly from a void pointer to any other pointer, but it suprises me that it would allow you to also convert to void itself. C++ forbids this.

    Code:
    extern void *PrintHello1&#40;void *params&#41;; 
    extern void *PrintHello2&#40;void *params&#41;; 
    extern void *PrintHello3&#40;void *params&#41;; 
     
    typedef void &#40;*Func&#41;&#40;void *&#41;;
    You defined the function as returning a void pointer by including an asterisk before PrintHello. An asterisk can only be used to create a function pointer when there are parenthesis to group it with the function name - you don't need to do that there since on those lines you want to make prototypes and not pointers. The typedef line looks fine.
    \"Nifty News Fifty: When news breaks, we give you the pieces.\" - Sluggy Freelance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •