Przekazanie dwuwymiarowej tablicy typu int do funkcji

0

Witam piszę program w którym w funkcji main deklaruję tablicę rozmiaru [n]x[m]. Tworzę też funkcji displayMatrix, która ma wyświetlać wszystkie elementy przekazanej macierzy.
Poniżej kod źródłowy funkcji displayMatrix

void displayMatrix(int rows, int columns, int **a)
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++)
            printf("%6d", a[i][j]);
        printf("\n");
    }

w funkcji main deklaruję tablicę załóżmy rozmiaru [n]x[m] i przekazuję ją do funkcji displayMatrix

int matrix_c[A_ROW][B_COLUMNS] = { 0 };

   displayMatrix(A_ROW, B_COLUMNS, matrix_c);
   printf("\n\n\n");

Kompilator wyrzuca mi błąd argument typu int*[] jest niezgodny z argumentem typu int **.
Czy jest jakiś sposób, żeby przekazać tablicę dwuwymiarową do funkcji, czy trzeba w funkcji main utworzyć tablicę dynamicznie tzn przy użyciu malloc

3

Opcja a:

void displayMatrix(int rows, int columns, int a[][3])
{
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++)
            printf("%6d", a[i][j]);
        printf("\n");
    }
}

ale to Cię pewnie nie urządza.

Możesz też tak:

#include <stdio.h>

void displayMatrix(int rows, int columns, int *a)
{
    for (int i = 0; i < rows; i++) {

        for (int j = 0; j < columns; j++) {
                printf("%6d", a[j]);
        }
        printf("\n");
        a+=columns;
    }
}

int main()
{
    int x[3*3] = {1,2,3, 4,5,6, 7,8, 9};
    displayMatrix(3,3,x);
}

EDIT: ew. możesz skompilować jako C99 i użyć VLA

#include <stdio.h>

void displayMatrix(int rows, int columns, int a[rows][columns])
{
    for (int i = 0; i < rows; i++) {

        for (int j = 0; j < columns; j++) {
                printf("%6d", a[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int x[3][3] = {{1,2,3}, {4,5,6}, {7,8, 9}};
    displayMatrix(3,3,x);
}

Osobiście jestem zwolennikiem zapisywania takich tablic jako jednowymariowych a potem odpowiedniego iterowania, no ale to ja.
Możesz ew. popełnić takie cudo, ale to też wymaga C99 (którego zdajesz się używać sądząc po definicjach zmiennych w forze):


void displayMatrix(int rows, int columns, void *a)
{
    int (*iterable)[rows][columns] = a;

    for (int i = 0; i < rows; i++) {

        for (int j = 0; j < columns; j++) {
                printf("%6d", (*iterable)[i][j]);
        }
        printf("\n");
    }
}

EDIT2:
opcja hardkor bez C99 i type generic to zrobić to makrem, będzie +- tak, ale uwierz nie chcesz tego ;)

#define PRINT(type, x) PRINT##type(x)
#define PRINTint(x) do{ printf("%6d", (x)); } while(0)
#define PRINTdouble(x) do{ printf("%3lf ", (x)); }while(0)

#define DISPLAY_MATRIX(type, rows, cols, matrix) \
do{ \
    int i;\
    int j;\
    int r = rows; \
    int c = cols; \
    type (*iterable)[rows][cols] = (void*) matrix; \
    for (i= 0; i < rows; i++) { \
        for (j = 0; j < cols; j++) { \
                PRINT(type, (*iterable)[i][j]); \
        } \
        printf("\n"); \
    }\
}while(0)

int main()
{
    int x[3][3] = {{1,2,3}, {4,5,6}, {7,8, 9}};
    double y[5][2] = {{1., 3.}, {3.4, 3.2}, {1.1, 2.8}, {666, 42}, {12.2, 121}};


    DISPLAY_MATRIX(int, 3,3, x);
    DISPLAY_MATRIX(double, 5,2, y);

}
0

Dzięki za odpowiedzi. Kod działa

1 użytkowników online, w tym zalogowanych: 0, gości: 1