【发布时间】:2014-11-14 13:20:49
【问题描述】:
我想将变量中的字符放入 C 中的字符数组中。我还想在之后打印反转的数组,如您所见,但这不是现在的问题。
这是我目前得到的代码:
作为标准输入,我使用带有“
#include <stdio.h>
#include <stdlib.h>
int main()
{
int counter = 0;
char character_array[57];
int i = 0;
int j = 0;
char character = 0;
// While EOF is not encountered read each character
while (counter != EOF)
{
// Print each character
printf("%c", counter);
// Continue getting characters from the stdin/input file
counter = getchar(stdin);
// Put each character into an array
character_array[j] = { counter };
j = j + 1;
}
// Print the array elements in reverse order
for (i = 58; i > 0; i--)
{
character = character_array[i];
printf("%c", character);
}
return 0;
}
我的 IDE 在第 35 行的第一个花括号“预期表达式”之后说。
// Put each character into an array
character_array[j] = { counter };
所以我猜它在那里失败了。我假设我不能像这样将字符变量放在数组中?否则我该怎么做呢?
PS:我是 C 新手。
【问题讨论】:
-
您是否尝试删除
counter周围的{和}? -
character_array[j] = counter;但最好使用scanf( "%s", character_array );并一次读取整个字符串。请注意,您的 character_array 数组最好更长! -
和
getchar(stdin)-->getchar() -
谢谢大家,去掉花括号成功了!