help passing arguments
¡Hola amigos!
i receive an error in the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
#include <pthread.h>
using namespace std;
struct arg_struct {
int arg1;
int arg2;
};
void *print(void *arguments)
{
struct arg_struct *args = arguments;
cout << args -> arg1 << "\n";
cout << args -> arg2 << "\n";
pthread_exit(NULL);
return NULL;
}
int main()
{
pthread_t thr;
struct arg_struct args;
args.arg1 = 10;
args.arg2 = 20;
if (pthread_create(&thr, NULL, &print, (void *)&args) != 0) {
cout << "\nERROR\n\n";
return -1;
}
return pthread_join(thr, NULL);
}
|
the error is:
file.cpp: In function âvoid* print(void*)â:
file.cpp:14: error: invalid conversion from âvoid*â to âarg_struct*â
|
How can i solve it, please ?
You don't need to cast from "any pointer" to "void pointer" but you need to cast from "void pointer" to "any pointer".
struct arg_struct *args = static_cast<struct arg_struct *>(arguments);
Hi ,
Have you tried
struct arg_struct *args = (arg_struct*)&arguments;
also make the changes like
1 2 3 4 5 6 7 8
|
int* print(void *arguments)
{
struct arg_struct *args = arguments;
cout << args -> arg1 << "\n";
cout << args -> arg2 << "\n";
pthread_exit(NULL);
return NULL;
}
|
I think this will work
Topic archived. No new replies allowed.