我正在尝试通过重载函数传递数组。该数组是一串由空格分隔的数字。我正在尝试将 char 数组转换为 int 数组,因为我有另一个使用指向 char 的指针调用的函数。
这是我目前的解决方案。我试图将每个元素单独复制到一个 int 数组,但是当我尝试从该数组中读取时,我得到了一堆垃圾数字。它传递给我创建的正确重载函数。
char str[100]; //string with inputted ints
int numStr[100];
int i = 0;
int count = 0;
fgets(str, 100, stdin);
while (str[i] != '\0')
{
if (str[i] == ' ')
{
count++;
}
i++;
}
if (count >= 1) //checks if there is more then one int
{
for (int i = 0; str[i] != '\0'; i++)
{
numStr[i] = str[i];
numStr[i] = numStr[i] - '/0';
}
printf("Going into the int array function\n");
assessGrade(numStr); //passing int array
}
程序应该将空格之间的每个数字一起复制到另一个 int 数组中。
char str[100];
int numStr[100];
char *token;
int count = 0;
int spaceCounter = 0;
fgets(str, 100, stdin);
int i = 0;
while (str[i] != '\0')
{
if (str[i] == ' ')
{
spaceCounter++;
}
i++;
}
if (spaceCounter >= 1) //checks if there is more then one int
{
token = strtok(str, " ");
while (token != NULL)
{
//printf("%s \n", token);
numStr[count] = atoi(token);
token = strtok(NULL, " ");
count++;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句