int main(int argc, char *arv[])
{
printf("Enter sequence of numbers seperated by comma\n");
char input[200];
char *valueStr = NULL;
int total = 0;
int values[100];
int ctr = 0, i = 0;
if (fgets(input, sizeof(input), stdin) != NULL)
{ // split using comma
printf("printing %s", input);
valueStr = strtok(input, ",");
printf("printing %s", valueStr);
while (valueStr != NULL && sscanf(valueStr, "%d", values[ctr++]) != EOF);
{
printf("Got value %d\n", values[ctr - 1]);
valueStr = strtok(NULL, ",");
}
for (i = 0; i < ctr; i++)
total +=values[i];
if (total == 0)
printf("Equals to 0\n");
else
printf("Doesn't equal to 0\n");
}
}
对我来说似乎很简单......我做错了什么?目前输出如下:
bash-4.2$ ./subset
Enter sequence of numbers seperated by comma
-5,-3,-1,2,4,6
printing -5,-3,-1,2,4,6
Segmentation fault (core dumped)
它永远不会被传递给strtok(),我不明白为什么......我没有将字符串文字传递给函数!