简体   繁体   English

在 C 中打印出二维数组的子矩阵时结果不正确

[英]Incorrect results when printing out a submatrix of a 2D array in C

I'm new to C and learning about arrays.我是 C 的新手,正在学习 arrays。 I've made a function, print2d , to print out a two dimensional array.我制作了一个 function, print2d来打印出一个二维数组。 I've created a 9x9 array, array2d .我创建了一个 9x9 数组array2d First, I printed out array2d using print2d , which worked perfectly.首先,我使用print2d打印出array2d ,效果很好。 Then, I tried to print out a 3x3 submatrix of array2d that consists of elements in the first three rows and first three columns.然后,我尝试打印出array2d的 3x3 子矩阵,它由前三行和前三列中的元素组成。 The 3x3 submatrix is printed out incorrectly. 3x3 子矩阵打印不正确。

In main:主要:

int array2d[9][9] = {
        {0, 1, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {1, 1, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0, 0, 0, 0, 0}
    };
    print2d(rows,cols,array2d);

    print2d(3,3,array2d); // print 3x3 submatrix
void print2d(int rows, int cols, int array[][cols])
{
    printf("{");

    for (int r = 0; r < rows; r++)
    {
        printf("{%i",array[r][0]);
        for (int c = 1; c < cols; c++)
        {
            printf(", %i",array[r][c]);
        }
        printf("}\n");
    }
    printf("}\n");
}

Output: The 9x9 matrix array2d is printed out properly, but the 3x3 submatrix is printed out as: Output: 9x9 矩阵array2d打印正确,但 3x3 子矩阵打印为:

{{0, 1, 0}
{0, 0, 0}
{0, 0, 0}
}

instead of代替

{{0, 1, 0}
{0, 0, 0}
{1, 1, 0}
}

The first row of the submatrix is correct, but the third row is not.子矩阵的第一行是正确的,但第三行是不正确的。 My guess is that by calling print2d(3,3,array2d) , the function print2d is expecting a 2d array with 3 columns ( int array[][3] ), rather than a 9x9 array.我的猜测是,通过调用print2d(3,3,array2d) , function print2d期望一个具有 3 列( int array[][3] )的二维数组,而不是 9x9 数组。 I'm not sure what kind of problem that is causing, and why the first row of the output is correct but not the third.我不确定这是什么问题,为什么 output 的第一行是正确的,但第三行不正确。 Thank you for your help!谢谢您的帮助!

You can use double pointer instead of 2D array您可以使用双指针而不是二维数组

void print2d(int rows, int cols, int **array);

Or define the sub array as @Weather_Vane comment above:或者将子数组定义为上面的@Weather_Vane 注释:

void print2d(int rows, int cols, int array[rows][cols], int sub_row, int sub_col) {
   for (int r = 0; r < sub_row; r++)
    {
       ...
        for (int c = 1; c < sub_col; c++)
        {
          ...
        }
        ...
    }
    printf("}\n");
}

main function for array with sub array arguments主 function 用于带有子阵列 arguments 的阵列

print2d(9,9,array2d, 9, 9);
print2d(9,9,array2d, 3, 3);

main function for double pointer:双指针的主要 function:

int main() {
  int rows, cols, i;
  int **array2d;

  /* obtain values for rows & cols*/

  // allocate the array
  array2d = malloc(rows * sizeof(int));
  if (!array2d)
    return -1;
  for (i=0; i<rows; i++) {
    array2d[i] = malloc(cols * sizeof *array2d[i]);
    if(!array2d[i])
        return -1;
  }

  /*init the values of array here */

  print2d(rows,cols,array2d);
  for (i=0; i<rows; i++)
    free(array2d[rows];
  free(array2d);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM