鱼C论坛

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

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

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

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

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

x
以下是需要实现的程序图片
tu.png
下面代码是使用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;
}
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.
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;
}

想知道小甲鱼最近在做啥?请访问 -> 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.
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;
}

RESULR

RESULR
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

int main()
{
        int i = 0, j = 0;
        int array[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("%c ",array[i][j]);
                        j++;
                }
                putchar('\n');
                i++;
        }
        
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 04:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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