How to pass a 2d array through pointer in c [duplicate]

Your array is of type int[2][2] (“array of 2 array of 2 int”) and its name will decay to a pointer to its first element which would be of type int(*)[2] (“pointer to array of 2 int”). So your func needs to take an argument of this type:

void func(int (*ptr)[2]);
// equivalently:
// void func(int ptr[][2]);

Alternatively, you can take a reference to the array type (“reference to array of 2 array of 2 int”):

void func(int (&ptr)[2][2]);

Make sure you change both the declaration and the definition.

Leave a Comment