Arrays 初学者C程序只打印2d数组的最后一行

Arrays 初学者C程序只打印2d数组的最后一行,arrays,c,multidimensional-array,Arrays,C,Multidimensional Array,我写了一个简单的C程序,它输入并打印两个多维数组的结果,矩阵a和矩阵B。 然而,由于某种原因,当我试图打印矩阵的结果时,它只会重复返回每个矩阵的最后一行。 !https://i.stack.imgur.com/RFGBZ.png char input; int row, col; int matrixA[row][col]; int matrixB[row][col]; int matrixResult[row][col]; printf("

我写了一个简单的C程序,它输入并打印两个多维数组的结果,矩阵a和矩阵B。 然而,由于某种原因,当我试图打印矩阵的结果时,它只会重复返回每个矩阵的最后一行。 !https://i.stack.imgur.com/RFGBZ.png

   char input;
   int row, col;
   int matrixA[row][col];
   int matrixB[row][col];
   int matrixResult[row][col];
   
   printf("Addition, Subtraction, and Multiplication of Matrices\n");
   printf("Set row size of matrices A and B: ");
   scanf("%d", &row);
   printf("Set column size of matrices A and B: ");
   scanf("%d", &col);
   int i, j;
   for(i=0; i<row; i++) {
     for(j=0;j<col;j++) {
        printf("Enter value for matrixA[%d][%d]:", i, j);
        scanf("%d", &matrixA[i][j]);
     }
  }
   for(i=0; i<row; i++) {
     for(j=0;j<col;j++) {
        printf("Enter value for matrixB[%d][%d]:", i, j);
        scanf("%d", &matrixB[i][j]);
     }
  }
   printf("\n\n");
   
   printf("Row size of Matrices A and B is: %d\n", row);
   printf("Column size of Matrices A and B is: %d\n\n", col);
   
   printf("Elements of matrix A:\n");
  for(i=0; i<row; i++){
  for(j=0;j<col;j++){
        printf("The value of matrixA[%d][%d] is %d\t", i ,j , matrixA[i][j]);
        if(j==col-1){
           printf("\n");
        }
     }
  }
  
  printf("Elements of matrix B:\n");
  for(i=0; i<row; i++) {
     for(j=0;j<col;j++) {
        printf("%d ", matrixB[i][j]);
        if(j==col-1){
        printf("\n");
        }
     }
  }

   
   return 0;
   }````


I am still a beginner to the C language and programming in general, so any help to my questions would be much appreciated. Thank you!

EDIT: SOLVED
Thank you so much Vlad from Moscow and M Oehm for the help!
I defined the constant variables ROW and COL (not the same int row and col in the main function) with a maximum number of 6. I also initialized int row and col to 0. The beginning of the code should look like this now:
   ````
   #include <stdio.h>
   #include <stdlib.h>
   #define ROW 6
   #define COL 6

   int main() {
   int row = 0, col = 0;
   int matrixA[ROW][COL];
   int matrixB[ROW][COL];

   }
   ````
char输入;
int row,col;
整数矩阵[行][列];
int matrixB[行][列];
int matrixResult[行][列];
printf(“矩阵的加法、减法和乘法”);
printf(“设置矩阵A和B的行大小:”);
scanf(“%d”行和第行);
printf(“设置矩阵A和B的列大小:”);
scanf(“%d”列和列);
int i,j;

对于(i=0;i而言,您在读取矩阵的实际大小之前声明了矩阵。在实际读取它们之前,无法知道行和列的内部内容,因此基于它们声明任何内容都会导致未定义的行为


尝试以下操作:.

这些声明int row,col;int matrixA[row][col];int matrixB[row][col];int matrixResult[row][col];导致未定义的行为,因为变量行和列未初始化。C程序是连续的。在定义2d数组时,维度
col
row
都没有有意义的值。将其定义推迟到向用户询问维度之后。或者,制作矩阵“足够大”,比如说10×10,并强制用户输入
col
row
为正,但最多为10。谢谢你,弗拉德和M。将根据你的建议进行处理。我使用的代码是:
#include#include#include“matrix.h”#定义第6行#定义第6列int main(){char input;int row=0,col=0;int matrixA[row][col;int matrixB[ROW][COL];
现在一切正常!非常感谢。