0

I'm using C and I'd like to print these matrices with the printMatrix function. It's fine when I print A and B. But, I don't know how matrix C is printed. can anyone help?

#include <stdio.h>
void printMatrix(const int A[][3],const int col);

void main()
{
    int A[2][3] = { {1,2,3},{-1,5,-9} };
    int B[2][3] = { {1,2,3},{1,0,4} };
    int C[3][2] = { {-2,1},{1,0},{5,4} };

    printf("A\n");
    printMatrix(A,2,3);
    printf("B\n"); printMatrix(B,2,3);
    printf("\n");
    printf("C\n"); printMatrix(C,3,2);
}


void printMatrix(const int A[][3], const int row,const int col)
{
    int i, j;
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            printf("%d  ", A[i][j]);
        }
        printf("\n");
    }
}

2 Answers2

3

You have two problems both related to the wrong array type. They array declaration const int A[][3] when part of a function decays into a pointer to the first element. In this case the first element would be an array of type const int[3] and a pointer to such an element is written as const int(*)[3], so that's what A gets implicitly replaced with.

Now as it happens, C has stricter typing for pointers than it has for regular data. This means that you cannot pass a pointer to an array of a different size than [3], nor can you pass a pointer to an array which doesn't have const int elements.

The size problem can be solved by using a pointer to variable-length array (VLA) instead. The const problem can't be easily solved1), unfortunately, so the easiest solution is to simply drop const correctness. You could change your function to this and it will work:

void printMatrix(size_t row, size_t col, int A[row][col]);

(size_t is a more correct type than int to use when dealing with array sizes.)


1) Advanced topic: There are solutions that give both const correctness and type safety, but they are cumbersome. See Const correctness for array pointers?, the _Generic based macro in the answer by chux in particular.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Thanks for your answer, But it doesn't work yet, the error is Parameter not available.(int A[row][col]) , Constant expression is required, Cannot allocate an array of constant size 0. – XXX TELTACIO Mar 18 '21 at 01:21
  • @XXXTELTACIO You must use a standard C compiler. It seems like you are using an obsolete non-standard one from the 1990s, such as Visual Studio. Or maybe you compiled the code as C++ by accident. – Lundin Mar 18 '21 at 09:10
2

The problem is the type of argument A in

void printMatrix(const int A[][3], const int row,const int col);

The second dimension is expected to be 3, but the type of C array is int[3][2]. The second dimension is 2. The type of the argument does not match thus it will not work.

I suggest using Variable Length Arrays:

void printMatrix(const int row,const int col, const int A[row][col])
{
    int i, j;
    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            printf("%d  ", A[i][j]);
        }
        printf("\n");
    }
}

now you can print any 2d int array as:

printMatrix(3,2,C);

Edit

As noted in the comments the code will produce a warning. The simple solution is dropping const from A argument:

void printMatrix(const int row,const int col, int A[row][col]);

Alternatively, one can add const qualifiers to arrays A,B, and C.

    const int A[2][3] = { {1,2,3},{-1,5,-9} };
    const int B[2][3] = { {1,2,3},{1,0,4} };
    const int C[3][2] = { {-2,1},{1,0},{5,4} };
tstanisl
  • 13,520
  • 2
  • 25
  • 40
  • This only solves one part of the problem and won't compile. I posted a complete answer. – Lundin Mar 17 '21 at 09:30
  • @Lundin, Indeed. It produces warning in pedantic mode. Interesting finding, thanks. – tstanisl Mar 17 '21 at 09:35
  • The compiler must produce a diagnostic in any mode, since assigning an array-pointer-to-int to an array-pointer-to-const-int is a constraint violation. – Lundin Mar 17 '21 at 09:45