LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 09-19-2005, 11:05 AM   #1
dragondad
LQ Newbie
 
Registered: Aug 2005
Posts: 18

Rep: Reputation: 0
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.
 
Old 09-19-2005, 11:27 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You want your functions to return void instead of void *. So change:

extern void *PrintHello1(void *params);

to:

extern void PrintHello1(void *params);

...for all 3 functions.
 
Old 09-19-2005, 11:33 AM   #3
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Try changing this:

typedef void (*Func)(void *);

to this:

typedef void* (*Func)(void *);
 
Old 09-19-2005, 11:39 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Quote:
Originally posted by itsme86
You want your functions to return void instead of void *. So change:

extern void *PrintHello1(void *params);

to:

extern void PrintHello1(void *params);

...for all 3 functions.
I was going to suggest that at first too, but apparently pthread_create wants a start routine that returns void*.... It uses that return value as a parameter to pthread_exit(void *value_ptr);

Last edited by deiussum; 09-19-2005 at 11:40 AM.
 
Old 09-19-2005, 11:41 AM   #5
dragondad
LQ Newbie
 
Registered: Aug 2005
Posts: 18

Original Poster
Rep: Reputation: 0
Neither two of the suggestion work, since the PrintHello1 (to 3) will be thread function, so it must be defined as void *, but thanks for your reply.
I still wait to find the solution.
 
Old 09-19-2005, 11:42 AM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
My suggestion doesn't suggest changing the return type of your PrintHello1. Your typedef is a typedef to a function that returns a void not a void*, so you have a mismatch there.
 
  


Reply



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
(I know I am inept) what is a pointer function in C ? cigarstub Programming 3 09-27-2005 05:06 PM
function pointer table and compile error dragondad Programming 2 09-20-2005 06:05 PM
Function Pointer as an Argument in C trutnev Programming 5 05-24-2005 10:22 AM
void * pointer in function xailer Programming 23 01-16-2004 02:14 PM
c++ Pointer to Member Function Wondre Programming 0 02-15-2003 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration