XXX TELTACIO
XXX TELTACIO

Reputation: 11

How to print a few 2d arrays with a fuction?

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

Upvotes: 0

Views: 57

Answers (2)

tstanisl
tstanisl

Reputation: 14107

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

Upvotes: 2

Lundin
Lundin

Reputation: 213513

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.

Upvotes: 3

Related Questions