Difficulty passing struct through pthread_create

Chucky picture Chucky · Mar 28, 2012 · Viewed 7.1k times · Source

I have an array of structs and I intend to pass each element of the array into separate pthreads in a for loop.

Here's my struct:

struct arrayData{
int *a;
int *b;
int up, low;
}

Here's the pointer to the first struct and a malloc (don't know if I quite get what goes on here):

struct arrayData * instance;
        instance = malloc(sizeof(struct arrayData)*n);

Here's my call to pthread_create:

pthread_create( &thread[i], NULL, add, (void *)instance[i]);

And for that line I'm getting the message "Cannot convert to a pointer type".

What could be wrong with that line?

Answer

Matt Eckert picture Matt Eckert · Mar 28, 2012

You are trying to convert a struct to a pointer in the last parameter. You need to pass the the struct's address with &.

pthread_create( &thread[i], NULL, add, &instance[i]);

As jørgensen mentioned, the void * cast is unnecessary.