0

好的,所以我使用 FOR 或 WHILE 循环,它们可以单独工作,但是当我在它之后键入相同类型的循环时,它会编译但在运行后它会给我错误“分段错误(核心转储)”或有时“总线错误核心转储”

我现在正在使用 2 个二维数组,我需要制作通用矩阵程序。现在代码用于输入。如果我在 /* 和 */ 之间添加一个循环,剩下的循环工作正常!

#include <stdio.h>

int main()
{
int i, j, k, l; //I, J for array matrix a, K, L for matrix b
int a[i][j]; //matrix of size i by j
int b[k][l];
int rowa=0, cola=0, rowb=0, colb=0;
//rowa is ROW no. of array a and colb e.g Column no. for array b

printf("size of matrix a: ");
scanf("%d\n%d", &i, &j); //i is row, j is column

printf("size of matrix b: ");
scanf("%d\n%d", &k, &l); //k is row, l is column


while (rowa < i)
{
    while (cola < j)
    {
    statements
    }
statements
}

while (rowb < k)
{
        while (colb < l)
    {
    statements
    }
statements
}

return 0;
}

那我该怎么办?谢谢

4

1 回答 1

5

您正在使用导致未定义行为的未初始化变量:

int i, j, k, l; //I, J for array matrix a, K, L for matrix b
int a[i][j]; //matrix of size i by j
...
printf("size of matrix a: ");
scanf("%d\n%d", &i, &j); //i is row, j is column

您应该在声明之前进行初始化i,即ja[i][j]

printf("size of matrix a: ");
scanf("%d\n%d", &i, &j);
// initialize the matrix:
int a[i][j];
于 2013-10-16T12:02:06.593 回答