为什么编译器跳过我的第二个scanf循环?

为什么编译器跳过我的第二个scanf循环?,c,C,这是一个用于添加矩阵的程序,但编译器似乎跳过了第二个嵌套for循环中的第二个scanf void input_add_matrices() { printf("Input number of rows for matrices: "); scanf("%d", &nrow_matrix1); getchar(); printf("Input number of columns for matrices: "); scanf("%d", &ncolumn_matri

这是一个用于添加矩阵的程序,但编译器似乎跳过了第二个嵌套for循环中的第二个scanf

void input_add_matrices() {
    printf("Input number of rows for matrices: "); scanf("%d", &nrow_matrix1); getchar();
    printf("Input number of columns for matrices: "); scanf("%d", &ncolumn_matrix1); getchar();
    printf("\nInput elements for matrix 1:\n");
    for(i=0; i<nrow_matrix1; i++){
        for(j=0; j<ncolumn_matrix1; j++){
            scanf("%d", &element_matrix1[i][j]); getchar();
        }
    }
    nrow_matrix2=nrow_matrix1;
    ncolumn_matrix2=ncolumn_matrix2;
    printf("\nInput elements for matrix 2:\n");
    for(i=0; i<nrow_matrix2; i++){
        for(j=0; j<ncolumn_matrix2; j++){
            scanf("%d", &element_matrix2[i][j]); getchar();
        }
    }
}

矩阵2是空的。

我认为它是代码行
ncolumn\u matrix2=ncolumn\u matrix2
导致ncolumn\u matrix2为0,这样就不会进入循环

我认为这是代码行
ncolumn\u matrix2=ncolumn\u matrix2
导致ncolumn_matrix2为0,这样就不会进入循环

不需要调用
getchar
scanf
%d”
格式跳过了前导空格,其中包括换行符(我猜您会担心这一点)。即使我省略了“getchar”函数,它仍然会出现。在第二个循环上设置断点并调试代码。如果它停在那里,那么跳过这个循环的不是编译器。也许您的程序确实执行了您期望它执行的操作,但这并不意味着编译器正在跳过任何操作。不需要调用
getchar
scanf
%d”
格式跳过了前导空格,其中包括换行符(我猜您会担心这一点)。即使我省略了“getchar”函数,它仍然会出现。在第二个循环上设置断点并调试代码。如果它停在那里,那么跳过这个循环的不是编译器。也许你的程序做了你期望它做的事情,但这并不意味着编译器跳过了任何东西。在这种情况下,它显然是零,但它实际上取决于
ncolumn\u matrix2
最初是如何初始化的。如果之前未初始化,则值不确定。是的,谢谢。这绝对是我的错。我没有看到我按了2而不是1。这是一个打字错误。哈哈哈,如果它再次出现,请刷新stdinstream@Fedeco:请注意的复杂性。在本例中,它显然为零,但它实际上取决于
ncolumn_matrix2
最初是如何初始化的。如果之前未初始化,则值不确定。是的,谢谢。这绝对是我的错。我没有看到我按了2而不是1。这是一个打字错误。哈哈哈,如果它再次出现,请刷新stdinstream@Fedeco:要意识到问题的复杂性。
Input number of rows for matrices: 2
Input number of columns for matrices: 2

Input elements for matrix 1:
1
2
1
2

Input elements for matrix 2: