2016-10-26 9 views
0

である私はスレッドの呼び出しでエラーが発生しました:予想される「ボイド*(*)(*空)」は、引数の型

typedef struct shape{ 
    int height; 
    int width; 

} rectangle; 

// just to denote the rectangles where width > height 
typedef rectangle wider; 

wider all[3]={{5,10},{2,4},{7,9}}; 

としてCで定義された構造を持っていると私は高さを出力機能を持っており、幅

void funct(wider shape){ 
printf("height: %d, width %d", shape.height, shape.width); 
} 

は今、私はスレッドを作成することによって、それぞれの形状のためにこれを達成したいので、私はこの試みた:

pthread_t threads[sizeof(all)]; 
int count=0; 
for(count=0; count <sizeof(all);++count) 
{ 
    if(pthread_create(&threads[count], NULL, funct,(wider*)all[count])!=0) 
    { 
    printf("Error in creating thread: %d",count); 
    break; 
    } 
} 

int i=0; 
for(i=0; i<count; ++i) 
{ 
if(pthread_join(threads[i],NULL)!=0) 
    printf ("Eroor joining: %d"+i); 
} 

しかし、このショーエラー

expected 'void * (*)(void *)' but argument is of type 'void (*)(struct rectangle)' 

は私が

void funct(void *wide){ 
wider shape=(wider)wide; 
// print same as before 
} 

に私の機能を変更しようとしたが、これはまだ動作しません。私は何を間違えているのですか?

+0

'より広い形状= *(広い)ワイド;'、すべての[count] 'の代わりに'&all [count] 'を渡します。ポインタ。 – Ryan

+1

エラーメッセージを再度読んでください。スレッド関数*の引数はポインタ*ですか?ポインタが返されますか? –

+1

また、 'sizeof'演算子はオペランド*のサイズをバイト*で返します。 'sizeof(all)'の場合、 'sizeof(wide)* 3'バイトを返します。配列内の要素数ではありません。これは 'threads'配列のサイズに影響します。 –

答えて

0

pthread_create()は、void*を入力とし、void*を返す関数の引数が必要です。しかし、あなたの機能はどちらもしません。したがって、コンパイラは型の不一致について不平を言う。

代わりに機能を変更:

void* funct(void *arg){ 
    wider shape = *(wider*)arg; 
    printf("height: %d, width %d", shape.height, shape.width); 
    return NULL; 
} 

あなたの引数渡しは、別の問題があります。 all[count]のアドレスを渡すのではなく、widervoid*に変換しています。あなたのsizeof計算も間違っています。 allアレイに正しい番号のwiderを取得するには、sizeof(all[0])で除算する必要があります。

pthread_t threads[sizeof(all)/sizeof(all[0])]; 
int count=0; 
for(count=0; count <sizeof(all)/sizeof(all[0]);++count) 
{ 
    if(pthread_create(&threads[count], NULL, funct,&all[count])!=0) 
    { 
     printf("Error in creating thread: %d",count); 
     break; 
    } 
} 
関連する問題