문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top