我是 C 语言编程的新手,我正在尝试打印一个字符串数组。我可以很容易地输入,但是当它试图打印数组时,程序停止响应。感谢任何帮助,谢谢。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
int a, b;
char answer[256];
for(a=1; a<=20; a++)
{
scanf("%s", &answer[a]);
}
for(b=1; b<=20; b++)
{
printf("%s\n", answer[b]);
}
}
更新:感谢大家的快速回复,程序运行顺利!
最佳答案
int a, b;
char answer[20][256];//<- array of char[256]
for(a=0; a<20; a++)//<- 0 origin
{
scanf("%255s", answer[a]);//<- Reading of the string with a maximum 255 characters (One is reserved for the termination character('\0'))
}
for(b=0; b<20; b++)
{
printf("%s\n", answer[b]);
}
关于C 打印出一个字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30308627/