I am creating a thread for each request from the client to get the files on the server. Thread function normally gets a void pointer; but I need to give it a char pointer as a parameter and want it to be populated with the file names by the thread function.
Code creates a thread:
pt_ret = pthread_create(&thread_id, NULL, getfiles, (void*) thread_buff);
pthread_join(thread_id, pt_ret);
Def. of thread function:
void *getfiles(void *ptr) {
/* ... */
char buff[256]; // populating that local buffer with the file names
// ptr should be as a return of buff
}
I have tried different things but each time after that thread completes, thread_buff becomes just ‘Q’.
Just cast it to a char*, since you know it actually is a char*:
Or you could also just work on the buffer pointed by ptr, without copying it, so it’s accessible after the thread has ended: