I have a structure declared and allocated in this way
typedef {
char* a; char* b; int c; int d; FILE *e;
} t;
[...]
ready= malloc(sizeof(t));
strncpy (ready->a, ss1, length);
strncpy (ready->b, ss2, length);
ready->c= f; ready->d= g;
ready->e= fopen(file, "w");
that I want to pass to a thread with
pthread_create(thread_id, NULL, worker_start, &ready);
when I begin to do some stuff in the thread function it’s clear that the fields that I had initialized in the main before calling the create are not defined in the thread.
void* worker_start(void *param) {
t *current;
current = (t*) param;
...
}
What’s wrong with the code? Am I doing something bad here?
When you’re passing the pointer to the thread, you pass it as a pointer to pointer (due to you using the address-of operator
&). You don’t need to do that, and in the thread function you don’t treat it as a pointer-to-pointer but just plain pointer.Remove the ampersand (
&) when creating the thread and things should work much better.