pass 2d array to function

I want to pass

int x[4][4];

to function

void something(???){


}

what should i put on ??? part?
Hello codeback
Im not too experienced in C, but ill do what i can to help.


What you can do is pass the pointer of the array to the function, e.g.

1
2
3
4
5
void something ( int* integerArray )
{
    integerArray[2][3] = 232342;
    //blah blah blah code
}


To call the 'something' function from the main function with the array as the argument, you would do this...

1
2
int x[4][4];
something ( x );


Hope I was of some help,
SuperStinger
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>

#define ROW 2
#define COLUMN 3

int showArray(int (*a)[COLUMN]);

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

    return 0;
}

int showArray(int (*a)[COLUMN])
{
    for (int i = 0; i < ROW; ++i)
    {
        for (int j = 0; j < COLUMN; ++j)
        {
            printf("a[%d][%d] = %d \n", i, j, *(*(a + i) + j));
        }
    }

    return 0;
}
Last edited on
There are two possibilities. Either to declare the parameter as reference to a two-dimensional array or declare it as pointer to the element of a two-dimensional array that is a one dimensional array.
So select either such declaration


void something( int ( &a )[4][4] );

or such declaration

void something( int ( *a )[4], int n );

where n - is the first dimension of the original array.
Last edited on
Topic archived. No new replies allowed.