[問題] pthread傳遞多個參數問題
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
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
02/01 18:58, 2F
→
02/01 18:59, , 3F
02/01 18:59, 3F
→
02/01 19:00, , 4F
02/01 19:00, 4F
※ 編輯: mammon0425 來自: 114.24.124.245 (02/01 19:13)
→
02/01 19:14, , 5F
02/01 19:14, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章