[問題] pthread傳遞多個參數問題

看板C_and_CPP (C/C++)作者 (x貪婪)時間15年前 (2011/02/01 18:49), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
pthread我想要傳兩個參數進去 所以用了struct包了起來 下面是我寫的程式碼 在最下面有發生錯誤 錯誤內容是 invaild initializer 應該要如何修正呢? 以下為程式碼----(有一行過長要換頁去看) #include <stdio.h> #include <stdlib.h> #include <pthread.h> .... struct arg_type { int number_of; char name_of[32]; }; void *consumer_function(void *consumer); .... int main(int argc,char* argv[]) { int i; ..... struct arg_type consumer_use_main; ..... pthread_t consumer_id[argc-1]; for(i=0;i<argc-1;i++) { consumer_use_main.number of = i; strcpy(consumer_use_main.name_of,argv[i+1]; pthread_create(&consumer_id[i],Null, consumer_function,(void *)& consumer_use_main); } ...... } void *consumer_function(void *data) { struct arg_type consumer_use = (struct arg_type*)data;//錯誤在這一行 ..... } P.S.我是照http://pccts.blogspot.com/2007/11/pthreadcreate.html的內容寫的 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.24.124.245 ※ 編輯: mammon0425 來自: 114.24.124.245 (02/01 18:55)

02/01 18:57, , 1F
struct arg_type* consumer_use = (struct arg_type*)
02/01 18:57, 1F
謝謝~編譯過了..... 想再問一下這樣調整後該如何取consumer_use裡的值.... 我用 printf("%s,%d\n",consumer_use.name_of,consumer_use.number_of); 回報錯誤是 request for member 'number_of'in somthing not a structure or union request for member 'name_of'in somthing not a structure or union

02/01 18:58, , 2F
POD初始化用{}, 因為沒有定義 operator= 所以報錯, 改
02/01 18:58, 2F

02/01 18:59, , 3F
用 memcpy 就能達成賦值的語意了
02/01 18:59, 3F

02/01 19:00, , 4F
疑 原來是要用指標接 XD
02/01 19:00, 4F
※ 編輯: mammon0425 來自: 114.24.124.245 (02/01 19:13)

02/01 19:14, , 5F
consumer_use->number_of
02/01 19:14, 5F
文章代碼(AID): #1DH-KXmT (C_and_CPP)
文章代碼(AID): #1DH-KXmT (C_and_CPP)