Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-27-2004, 04:54 PM   #1 (permalink)
Lovecraft
Registered User
 
Join Date: Apr 2004
Posts: 3
Lovecraft is on a distinguished road
void * conversion problems

I really need some help here. I'm writing a program in c++ on a unix system, using the pthreads package. This here line:

pthread_create(&cars[i],NULL,Car, (void *) i);

is giving me the error:

invalid conversion from `void (*)(int)' to `void*(*)(void*)'

Now, I've done the same thing in another program, and it works just fine, but this won't work. I'm using g++ to compile. Can someone help me out?
Lovecraft is offline   Reply With Quote
Old 04-27-2004, 05:27 PM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
well, since you didn't post declaration of Car() and "i", i will speculate. you are trying to pass a function similar to this:

void Car(int i);

as the third argument of pthread_create. this is incorrect, don't do that.
joe_bruin is offline   Reply With Quote
Old 04-27-2004, 05:36 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
Originally posted by joe_bruin
well, since you didn't post declaration of Car() and "i", i will speculate. you are trying to pass a function similar to this:

void Car(int i);

as the third argument of pthread_create. this is incorrect, don't do that.
What he does is perfectly legal as long as there is a "car function" taking a void * parameter and returns a void* exit status.

But if I remember correctly:
(void *) i
should be replaced by
&i
Assuming "i" is just an integer.
__________________
Valmont is offline   Reply With Quote
Old 04-27-2004, 05:43 PM   #4 (permalink)
Lovecraft
Registered User
 
Join Date: Apr 2004
Posts: 3
Lovecraft is on a distinguished road
void *Car(void *arg)

is the car function. I tried &i, but that gave the same error. If seeing more code helps, lemme know, I'll post whatever you need to see. I really am at my wits end.
Lovecraft is offline   Reply With Quote
Old 04-27-2004, 06:18 PM   #5 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
the error message means the following:
"cannot convert a function pointer for a function that takes in an int and returns a void to a function pointer that takes in a void* and returns a void*".

this code compiles fine.

Code:
#include <pthread.h>

void *Car(void *arg)
{
  return NULL;
}

int main()
{
  pthread_t cars[5];
  int i = 0;

  // your call
  pthread_create(&cars[i],NULL,Car, (void *) i);

  return 0;
}
this code generates your error:
Code:
#include <pthread.h>

void Car(int arg)
{
}

int main()
{
  pthread_t cars[5];
  int i = 0;

  // your call
  pthread_create(&cars[i],NULL,Car, (void *) i);

  return 0;
}
p.c: In function `int main()':
p.c:13: invalid conversion from `void (*)(int)' to `void*(*)(void*)'

have a look at Car again, something is strange about it (maybe you have a prototype in a header file that doesn't match the body).

btw, there is nothing wrong with "(void *)i" if you intend to have a pointer who's value is i (i'm guessing your Car function will take this value and convert it back into an int).
joe_bruin is offline   Reply With Quote
Old 04-27-2004, 06:37 PM   #6 (permalink)
Lovecraft
Registered User
 
Join Date: Apr 2004
Posts: 3
Lovecraft is on a distinguished road
doh!

Gah, I found the problem.

I had a function prototype like this:

void Car(int cid);

but the function was this:

void *Car(void *car);

I forgot to change the prototype when I started to use pthreads.

It compiles fine now. Thanks for the suggestion to check my prototype, I never would have thought to check it. I appreciate the help, thanks so much.
Lovecraft is offline   Reply With Quote
Old 04-28-2004, 03:51 AM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
...as long as there is a "car function" taking a void * parameter and returns a void* exit status...
Lol thanks Joe. I have no time actually
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
2D graphic repaint logic ralphtan Java 1 08-13-2004 03:49 AM
more help needed kashif Standard C, C++ 0 04-21-2003 10:30 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 08:50 AM
I'm having some problems with inheritance... <-- newb mik0rs Standard C, C++ 5 04-08-2003 10:54 PM


All times are GMT -8. The time now is 04:29 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting