求助C语言循环
以下是需要实现的程序图片下面代码是使用for循环来实现以上程序,没有任何问题;
#include <stdio.h>
int main()
{
int array;
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
array = getchar();
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf("%c ",array);
}
putchar('\n');
}
return 0;
}
以下是我使用while循环来实现图片程序,出问题了,接下来是代码,请指出到底是哪里的问题,在此先谢过了
#include <stdio.h>
int main()
{
int i = 0, j = 0;
int array;
while (i < 3)
{
j = 0;
while (j < 3)
{
array = getchar();
j++;
}
i++;
}
i = 0;
while (i < 3)
{
j = 0;
while (j < 3)
{
printf("%c ",array);
j++;
}
putchar('\n');
i++;
}
return 0;
}
本帖最后由 xypmyp 于 2019-1-8 18:41 编辑
It because you declared the array. Which mean you did not allocated memory correctly.
change to int array; 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; //change to arrar;
while (i < 3){
j = 0;
while (j < 3){
array = getchar();
j++;
}
i++;
}
i = 0;
while (i < 3){
j = 0;
while (j < 3){
printf("%1c ",array);
j++;
}
putchar('\n');
i++;
}
return 0;
}
数组有固定长度吧。
#include <stdio.h>
int main()
{
int i = 0, j = 0;
int array;
while (i < 3)
{
j = 0;
while (j < 3)
{
array = getchar();
j++;
}
i++;
}
i = 0;
while (i < 3)
{
j = 0;
while (j < 3)
{
printf("%c ",array);
j++;
}
putchar('\n');
i++;
}
return 0;
}
页:
[1]