Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke function with 2D array as parameter [duplicate]

Tags:

c

I'm Java developer, not a C programmer. I'm trying to invoke some function that receives a 2D array, tried like:

#include <stdio.h>
int foo(int mat[][N])
{ 
  printf("%d",N); /*  <--- here error */

}

int main()
{
    int a[5][5] =
    {
        {1, 2, 3, 4,5},    
        {1, 5, 5, 5,2},    
        {1, 4, 30,4,2},
        {1, 2, 2,2, 2},
        {1,1, 1,1,  2 } 
    };

    foo(a);
    return 0;
}

But the compiler says:

'N' undeclared (first use in this function)

I must have the signature: int foo(int mat[][N])

How should I pass the 2D array then?

like image 897
Devy Avatar asked Dec 10 '18 18:12

Devy


1 Answers

You can use macros to define constant numbers, i.e. #define N 5. At compile time each appearance of the defined macro name will be replaced with the given value. In our case each N would be replaced with 5. But this would not solve your problem, because it would print 5 every time, even if you change the dimensions of your array.

Pass the dimensions of the array as parameters.

You can pass the 2D array as a pointer.

I created a printing function, with what I can show you accessing the elements from the array. So the function's prototype would look like:

void print2DMatrix(int *matrix2D, const int rowLength, const int columnLength);

where matrix2D is a pointer, there will be passed the address of the array. The other two parameters tell us the dimensions of the array.

We can access the elements in a selected row and in a selected column with matrix2D[selectedRow * maxRowLength + selectedColumn].

When you call the function, you can pass the name of the array, like print2DMatrix(*myMatrix, myRowLength, myColumnLength);, of course you shall declare and initialize it before using. print2DMatrix(myMatrix, myRowLength, myColumnLength); would cause warning, because the passed argument and the waited one have different types, but it will run correctly. The passed one is a double pointer, when the waited one is a single pointer. Because of that you have to use print2DMatrix(*myMatrix, myRowLength, myColumnLength);, where *myMatrix will point to the first row of our array.

I would like to mention that myMatrix and *myMatrix point to the same address, the difference is: myMatrix is looked as a double pointer to an integer, when *myMatrix is looked as a pointer to an integer. Run printf("%d %d %d", myMatrix, *myMatrix, **myMatrix); to see the result. **myMatrix will point to the first element of the first row, what is 11. See the entire code below...

#include <stdio.h>

#define ROW_LENGTH      5
#define COLUMN_LENGTH   5

void print2DMatrix(int *matrix2D, const int rowLength, const int columnLength)
{
    int i;
    for (i = 0; i < rowLength; i++)
    {
        int j;
        for (j = 0; j < columnLength; j++)
        {
            printf("%d ", matrix2D[i * rowLength + j]);
        }
        printf("\n");
    }
}

int main(void)
{
    const int myRowLength = ROW_LENGTH;
    const int myColumnLength = COLUMN_LENGTH;
    int myMatrix[ROW_LENGTH][COLUMN_LENGTH] =
    {
        {11, 12, 13, 14, 15},
        {21, 22, 23, 24, 25},
        {31, 32, 33, 34, 35},
        {41, 42, 43, 44, 45},
        {51, 52, 53, 54, 55}
    };

    print2DMatrix(*myMatrix, myRowLength, myColumnLength);

    return 0;
}
like image 95
Norbert Incze Avatar answered Oct 23 '22 14:10

Norbert Incze