• Advertisement
Sign in to follow this  

pthreads: Passing a char* array

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement