現在、私のプログラムは、コマンドラインでユーザーが入力した数値のリストを取得し、この数値の合計を見つけて出力する必要があります。私のコードは次のとおりです。ユーザーが入力した単一の数値を保存することはわかっていますが、スペースで区切られた数値のリストが必要な場合はどうすればよいでしょうか?
#include <pthread.h>
#include <stdio.h>
int sum; /* this data is shared by the thread(s) */
void *runner(char **); /* threads call this function */
int main(int argc, char *argv[])
{
pthread_t tid; /* the thread identifier */
pthread_t tid2;
pthread_attr_t attr; /* set of thread attributes */
if (argc != 2) {
fprintf(stderr,"usage: a.out <integer values>\n");
return -1;
}
pthread_attr_init(&attr);
pthread_create(&tid,&attr,(void(*)(void *))(runner),(void *)(argv+1));
pthread_join(tid,NULL);
printf("sum = %d\n",sum);
}
/* The thread will begin control in this function */
void *runner(char **param)
{
int i;
sum = 0;
for (i = 1; i <= 5; i++)
sum = sum + atoi(param[i]);
pthread_exit(0);
}
コマンドラインに数値のリストを入力し、それらの数値をリストに保存してから、それらすべての数値の合計を見つけられるようにしたいと考えています。
、誰かがこれを行う正しい方法を教えてもらえますか?