【发布时间】:2014-07-15 10:47:36
【问题描述】:
我需要把argv中的所有参数都用在不同的porpuse上,程序入口是这样的
./program -t <a number> -d <a string> -m < a char, e or l >
IE:./program -t 10 -d have/a/nice/day -m e
我做的是这样的:
int main(int argc, char *argv[]){
int i,nt;
char aux[6];
for (i=0;i<6;i++){
aux[i] = *((char*)argv[i+1]);
}
nt=(int)aux[1]-'0';
printf("%d",nt);
printf("%c",aux[1]);
pthread_t threads[nt];
if (aux[5]=='e'){
printf("mode ejec\n");
/*for (i=0;i<nt;i++){
pthread_create (&threads[i],NULL,(void *)operacion,NULL);
for (i=0;i<nt;i++){
pthread_join (threads[i],NULL);
}
}*/
}else if(aux[5]=='l'){
printf("mode lib\n");
}
return 0;
}
但是当我在 argv 中占据任何位置时,只显示参数的第一个字符/数字
在示例中,它将显示:
[-,1,-,h,-,e]
我怎样才能获取所有元素?
对不起,如果我没有解释得那么好......英语不是我的主要语言
【问题讨论】:
-
正弦
aux数组定义为char,aux[i]只能容纳单个字符。不知道为什么需要它,因为argv本身已经是一个可以直接使用的字符串数组。有关字符串数组的更多信息,请参阅c.learncodethehardway.org/book/ex10.html。
标签: c multithreading argv