[英]I want to store the values(numbers) of the comma separated string in a number array and set all empty values to 0 in C
我想将所有这些值存储在一个数字数组中输出应该为014182 70 90 0 0
等等……我得到一个非常有趣的输出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH_OF_NUMBER 9
char *string ;
char *comma ;
char *position ;
char total ;
int scores[42], i, j ;
char number[MAX_LENGTH_OF_NUMBER + 1] ;
int main()
{
string = "014182,70,90,,,,,89,,69,76,80,,,80,,100,,76,,,,,,,,,,,,,,,,,,,,,,90," ;
comma = strchr (string, ',') ;
position = string ;
while (comma)
{
i, j = 0 ;
while (position < comma) {
number[i] = *position ;
i++ ;
position++ ;
}
number[i] = '\0' ;
position++ ;
comma = strchr (position, ',') ;
scores[j] = atoi (number) ;
printf("%d\n", scores[j]) ;
j++ ;
}
}
这是一个很大的问题:
i, j = 0 ;
感谢逗号运算符 ,实际上这是两个不同的表达式:
i
和
j = 0
简单地忽略表达式i
,然后对j
进行赋值。
我想你想做
i = 0;
j = 0;
需要注意的是,循环顶部j
的赋值意味着您将一遍又一遍覆盖相同(第一个) scores
输入。
您还应该进行一些边界检查,以使j
不会变大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.