2015-08-15 109 views
0

您好,我試圖創建一個線程調用一個函數,它需要一個結構。 問題現在面臨的是GCC告訴我一個不兼容的指針創建線程不兼容的類型

這是錯誤

warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [enabled by default] 
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(struct sonicPins *) 

的代碼與此錯誤 pthread_create(&thr1, NULL, &threadFunc,(void*) &args1)線;

下面是函數其試圖調用

void* threadFunc(struct sonicPins *args) 

的結構也是在sonicThread.h定義的struct

以上主要功能的要點我有

struct sonicPins *args1; 

在我主我

args1 = malloc(sizeof(struct sonicPins)); 

args1->trig1 = 21; 
args1->echo1 = 20; 
//front right. 
args1->trig2 = 16; 
args1->echo2 = 12; 
//rear left; 
args1->trig3 = 26; 
args1->echo3 = 19; 
//rear right. 
args1->trig4 = 13; 
args1->echo4 = 6; 

我嘗試過各種方法來解決這個問題,但找不到解決問題的方法。

+0

我回滾您的編輯,因爲它由以下毫無意義的答案。獲得答案後請不要更改代碼。發佈新問題或添加更新後的代碼而不刪除舊代碼。 –

回答

3

GCC的錯誤信息非常簡單:pthread_create方法需要一個接受void*參數的方法。

改變你的函數是:

void* threadFunc(void* sonicPinsPtr) { 
    struct sonicPins* args = sonicPinsPtr; 

    // rest of your code here 
} 
+0

我確實有這樣的感覺,但發現我無法訪問該結構。在被調用的函數內部,我有echo = args-> echo1; Thiis返回以下錯誤:sonicThread.c:在函數 '的ThreadFunc': sonicThread.c:11:8:錯誤: '迴響' 未聲明(第一在此函數使用) sonicThread.c:11:8:注:每個11:19:警告:未聲明的標識符被用於它出現在 sonicThread.c每個功能只報告一次解除引用「無效*」指針[默認啓用] sonicThread.c:11:19:錯誤:請求構件「ECHO1 '在某些不是結構或工會 –

+0

@JamesSmith那麼你應該顯示更多的代碼,因爲這是你的初始問題的*正確的*答案 - 再次看看這個答案中的代碼,當然你需要一個指向你的指針'struct'第一個... –

+0

@JamesSmith:然後在線程函數內部:'struct sonicPins * p = sonicPinsPtr;'並使用'p'訪問'struct sonicPins'的成員像這樣'對 - > echo1'。 – alk