Sign in to follow this  
amt35

pthreads: Passing a char* array

Recommended Posts

Using pthreads in C you have to pass your parameters as a (void *). My problem is I don't know how to pass a char* array as a (void *) and then convert it back. So in my main program I do this: char* thread_param[2]; thread_param[0] = .. thread_param[1] = .. ... ... pthread_create(..,..,mythead,(void*)thread_param); ... } But then in the thead how do I convert "args" back to a char* thread_param[2]? void mythread(void* args){ char* thread_param[2] = ? } Ideas?? Thanks!!

Share this post


Link to post
Share on other sites
Do the reverse of when you casted the array to void *.

char *array = (char *)thread_param;

The [] operator is used when declaring or accessing an element of an array. A one-dimensional array is essentially a char *.

Kuphryn

Share this post


Link to post
Share on other sites
It's an array of char*'s though so you'd need a double pointer... but you lose the size information [2] along the way.

The easy thing to do (in C) is wrap it in a struct...

typedef struct
{
char* params[2];
} MYTHREADPARAMS;

and pass the address of an instance of the struct.

In C++, you'd pass the address of a std::vector of std::string.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this