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.
- 09-19-2005 #1Just 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.
- 09-19-2005 #2
Shouldn't you be using:
Unless your functions really do return a void*, in which case you should set your typedef to 'void* Func(void*);'Code:typedef void Func(void *); struct PrintHi { int msg; Func* funcptr; };
Linux user #126863 - see http://linuxcounter.net/
- 09-19-2005 #3Linux Newbie
- Join Date
- Nov 2004
- Location
- New York
- Posts
- 150
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:invalid conversion from `void*(*)(void*)' to `void (*)(void*)'
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.Code:extern void *PrintHello1(void *params); extern void *PrintHello2(void *params); extern void *PrintHello3(void *params); typedef void (*Func)(void *);
\"Nifty News Fifty: When news breaks, we give you the pieces.\" - Sluggy Freelance


Reply With Quote
