马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下是需要实现的程序图片
下面代码是使用for循环来实现以上程序,没有任何问题;#include <stdio.h>
int main()
{
int array[3][3];
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
array[i][j] = getchar();
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%c ",array[i][j]);
}
putchar('\n');
}
return 0;
}
以下是我使用while循环来实现图片程序,出问题了,接下来是代码,请指出到底是哪里的问题,在此先谢过了#include <stdio.h>
int main()
{
int i = 0, j = 0;
int array[i][j];
while (i < 3)
{
j = 0;
while (j < 3)
{
array[i][j] = getchar();
j++;
}
i++;
}
i = 0;
while (i < 3)
{
j = 0;
while (j < 3)
{
printf("%c ",array[i][j]);
j++;
}
putchar('\n');
i++;
}
return 0;
}
本帖最后由 xypmyp 于 2019-1-8 18:41 编辑
It because you declared the array[0][0]. Which mean you did not allocated memory correctly.
change to int array[3][3]; it properly fixed the problem.
In C language, you can not use variable to allocated the memory size, use malloc() instead.
int main()
{
int i = 0; int j = 0;
int array[3][3]; //change to arrar[3][3];
while (i < 3){
j = 0;
while (j < 3){
array[i][j] = getchar();
j++;
}
i++;
}
i = 0;
while (i < 3){
j = 0;
while (j < 3){
printf("%1c ",array[i][j]);
j++;
}
putchar('\n');
i++;
}
return 0;
}
|