鱼C论坛

 找回密码
 立即注册
查看: 1752|回复: 2

[已解决]求助C语言循环

[复制链接]
发表于 2019-1-8 18:11:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以下是需要实现的程序图片
tu.png
下面代码是使用for循环来实现以上程序,没有任何问题;
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int array[3][3];
  5.         int i, j;
  6.        
  7.         for (i = 0; i < 3; i++)
  8.         {
  9.                 for (j = 0; j < 3; j++)
  10.                 {
  11.                         array[i][j] = getchar();
  12.                 }
  13.         }
  14.         for (i = 0; i < 3; i++)
  15.         {
  16.                 for (j = 0; j < 3; j++)
  17.                 {
  18.                         printf("%c ",array[i][j]);
  19.                 }
  20.                 putchar('\n');
  21.         }
  22.        
  23.         return 0;
  24. }
复制代码


以下是我使用while循环来实现图片程序,出问题了,接下来是代码,请指出到底是哪里的问题,在此先谢过了
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int i = 0, j = 0;
  5.         int array[i][j];
  6.        
  7.         while (i < 3)
  8.         {
  9.                 j = 0;
  10.                 while (j < 3)
  11.                 {
  12.                         array[i][j] = getchar();
  13.                         j++;
  14.                 }
  15.                 i++;
  16.         }
  17.        
  18.         i = 0;
  19.         while (i < 3)
  20.         {
  21.                 j = 0;
  22.                 while (j < 3)
  23.                 {
  24.                         printf("%c ",array[i][j]);
  25.                         j++;
  26.                 }
  27.                 putchar('\n');
  28.                 i++;
  29.         }
  30.        
  31.         return 0;
  32. }
复制代码

tu1.png
最佳答案
2019-1-8 18:39:16
本帖最后由 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.

  1. int main()
  2. {
  3.         int i = 0; int j = 0;
  4.         int array[3][3];                //change to arrar[3][3];
  5.        
  6.         while (i < 3){
  7.                 j = 0;
  8.                 while (j < 3){
  9.                         array[i][j] = getchar();
  10.                         j++;
  11.                 }
  12.                 i++;
  13.         }
  14.         i = 0;
  15.         while (i < 3){
  16.                 j = 0;
  17.                 while (j < 3){
  18.                            printf("%1c ",array[i][j]);
  19.                            j++;
  20.                 }
  21.                 putchar('\n');
  22.                 i++;
  23.         }
  24.         return 0;
  25. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-8 18:39:16 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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.

  1. int main()
  2. {
  3.         int i = 0; int j = 0;
  4.         int array[3][3];                //change to arrar[3][3];
  5.        
  6.         while (i < 3){
  7.                 j = 0;
  8.                 while (j < 3){
  9.                         array[i][j] = getchar();
  10.                         j++;
  11.                 }
  12.                 i++;
  13.         }
  14.         i = 0;
  15.         while (i < 3){
  16.                 j = 0;
  17.                 while (j < 3){
  18.                            printf("%1c ",array[i][j]);
  19.                            j++;
  20.                 }
  21.                 putchar('\n');
  22.                 i++;
  23.         }
  24.         return 0;
  25. }
复制代码


RESULR

RESULR
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-8 19:36:24 | 显示全部楼层
数组有固定长度吧。
  1. #include <stdio.h>

  2. int main()
  3. {
  4.         int i = 0, j = 0;
  5.         int array[3][3];
  6.         
  7.         while (i < 3)
  8.         {
  9.                 j = 0;
  10.                 while (j < 3)
  11.                 {
  12.                         array[i][j] = getchar();
  13.                         j++;
  14.                 }
  15.                 i++;
  16.         }
  17.         
  18.         i = 0;
  19.         while (i < 3)
  20.         {
  21.                 j = 0;
  22.                 while (j < 3)
  23.                 {
  24.                         printf("%c ",array[i][j]);
  25.                         j++;
  26.                 }
  27.                 putchar('\n');
  28.                 i++;
  29.         }
  30.         
  31.         return 0;
  32. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-15 10:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表