Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

instance is of type struct arrayData *, hence instance[i] is of type struct arrayData, which is an aggregate, not a pointer. Intended use was probably

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

The cast, btw, is pointless.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top