简体   繁体   中英

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

I'm new to C and learning about arrays. I've made a function, print2d , to print out a two dimensional array. I've created a 9x9 array, array2d . First, I printed out array2d using print2d , which worked perfectly. Then, I tried to print out a 3x3 submatrix of array2d that consists of elements in the first three rows and first three columns. The 3x3 submatrix is printed out incorrectly.

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:

{{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. 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. 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:

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

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

main function for double pointer:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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