什么是错误的,在我的代码二维数组从用户使用c [复制]输入

thtygnil  于 2023-03-07  发布在  其他
关注(0)|答案(1)|浏览(106)
    • 此问题在此处已有答案**:

Why is this VLA (variable-length array) definition unreliable?(1个答案)
23小时前关闭。

#include<stdio.h>

int main()
{
    int i,j,m,n;
    int a[m][n];

    printf("enter the row of matrix:");
    scanf("%d", &m);

    printf("enter the column of matrix:");
    scanf("%d",&n);

    printf("enter the elements\n");

    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d", &a[i][j]);
        }
    }

    printf("your matrix is :\n");

    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d", a[i][j]);
        }
    }
}

输出:

enter the row of matrix:3
enter the column of matrix:3
enter the elements
1
2
3
4
5
6
7
8
8
your matrix is :
788788788%

为什么我的输出只显示了第2行和第2列的3个输入。

e0bqpujr

e0bqpujr1#

int i,j,m,n;
int a[m][n];

mn在此处具有不确定的值。您需要在接收并验证输入后声明数组。
注意,scanf()返回它成功执行的转换次数,代码应检查它。

if (scanf("%d", &a[i][j]) != 1) { 
     complain ();
}

参见A beginner's guide away from scanf()

相关问题