I have a simple program to input the an array of 5 strings and output them. But the output is some what weird. The following is my code.
我有一个简单的程序来输入一个包含5个字符串的数组并输出它们。但输出有些奇怪。以下是我的代码。
#include <stdio.h>
int main()
{
char a[10][5];
int i;
for(i=0; i<5; ++i)
{
printf("\nEnter the name of %d st student:", i+1);
fflush(stdout);
gets(a[i]);
}
for(i=0; i<5; ++i)
{
printf("\n%s", a[i]);
fflush(stdout);
}
return 0;
}
I gave the inputs as tom, john, peter, david and alan and I get the following output.
我把输入作为tom,john,peter,david和alan给出了以下输出。
tom
john
peterdavidalan
davidalan
alan
What could be the problem?
可能是什么问题呢?
2 个解决方案
#1
7
The C array syntax is pretty confusing IMO - it means the opposite of the way I typically read it... and it looks like you made that mistake too.
C数组语法非常混乱IMO - 它意味着我通常阅读它的方式相反......而且看起来你也犯了这个错误。
Your array has ten slots of strings 4 chars each (it isn't 5 because each string in C must end with a 0 character). So when you enter peter
, instead of comfortably fitting in a 10 char buffer, it overflows a 5 char buffer, saving over one of the characters saved previously.
你的数组有10个字符串的每个字符串4个字符(它不是5个,因为C中的每个字符串必须以0字符结尾)。因此,当您输入peter时,它不会舒适地适应10个字符缓冲区,而是溢出5个字符缓冲区,从而节省了之前保存的一个字符。
Without the 0 terminator, printf will just keep going, and thus write the other names too.
如果没有0终止符,printf将继续运行,因此也会编写其他名称。
#2
2
just change your char a[10][5];
to char a[5][10];
you will have then 5 rows with 10 columns each. current setting of a
lets you hold 10 inputs of 4 characters long (since you need a \0
character at the end of string).
C doesn't check for boundaries and multidimensional arrays are stored continguously in memory. therefore, with input longer than 4 characters you are overflowing the current storage for your char array and writing to next row.
Adam D. Ruppe mentioned that printf()
will be printing chars to screen until it meets the \0
terminator. see that you are:
只是改变你的角色[10] [5]; char char [5] [10];你将有5行,每行10列。 a的当前设置允许您保存10个4个字符长的输入(因为您需要在字符串末尾添加\ 0字符)。 C不检查边界,多维数组以内存方式存储。因此,如果输入超过4个字符,则会溢出char数组的当前存储并写入下一行。 Adam D. Ruppe提到printf()将打印字符到屏幕,直到它遇到\ 0终止符。看你是:
- loosing terminator with "peter" input
- 用“彼得”输入失去终结者
- loosing terminator with "david" input
- 用“大卫”输入丢失终结者
hence you get the output "peterdavidalan". now i think you can figure out from where "davidalan" output came.
因此你得到输出“peterdavidalan”。现在我想你可以弄清楚“davidalan”输出来自哪里。
#1
7
The C array syntax is pretty confusing IMO - it means the opposite of the way I typically read it... and it looks like you made that mistake too.
C数组语法非常混乱IMO - 它意味着我通常阅读它的方式相反......而且看起来你也犯了这个错误。
Your array has ten slots of strings 4 chars each (it isn't 5 because each string in C must end with a 0 character). So when you enter peter
, instead of comfortably fitting in a 10 char buffer, it overflows a 5 char buffer, saving over one of the characters saved previously.
你的数组有10个字符串的每个字符串4个字符(它不是5个,因为C中的每个字符串必须以0字符结尾)。因此,当您输入peter时,它不会舒适地适应10个字符缓冲区,而是溢出5个字符缓冲区,从而节省了之前保存的一个字符。
Without the 0 terminator, printf will just keep going, and thus write the other names too.
如果没有0终止符,printf将继续运行,因此也会编写其他名称。
#2
2
just change your char a[10][5];
to char a[5][10];
you will have then 5 rows with 10 columns each. current setting of a
lets you hold 10 inputs of 4 characters long (since you need a \0
character at the end of string).
C doesn't check for boundaries and multidimensional arrays are stored continguously in memory. therefore, with input longer than 4 characters you are overflowing the current storage for your char array and writing to next row.
Adam D. Ruppe mentioned that printf()
will be printing chars to screen until it meets the \0
terminator. see that you are:
只是改变你的角色[10] [5]; char char [5] [10];你将有5行,每行10列。 a的当前设置允许您保存10个4个字符长的输入(因为您需要在字符串末尾添加\ 0字符)。 C不检查边界,多维数组以内存方式存储。因此,如果输入超过4个字符,则会溢出char数组的当前存储并写入下一行。 Adam D. Ruppe提到printf()将打印字符到屏幕,直到它遇到\ 0终止符。看你是:
- loosing terminator with "peter" input
- 用“彼得”输入失去终结者
- loosing terminator with "david" input
- 用“大卫”输入丢失终结者
hence you get the output "peterdavidalan". now i think you can figure out from where "davidalan" output came.
因此你得到输出“peterdavidalan”。现在我想你可以弄清楚“davidalan”输出来自哪里。