C Programming - Passing a multi-dimensional array to a function
Posted on March 27, 2019 by Paul
In this article I will show you how to pass a multi-dimensional array as a parameter to a function in C. For simplicity, we will present only the case of 2D arrays, but same considerations will apply to a general, multi-dimensional, array.
There is also a video version of this tutorial:
In C, a two-dimensional array is a one-dimensional array of one-dimensional arrays. For example, an array of two rows and 3 columns is technically a one-dimensional array of two elements, where each element is a one-dimensional 3 elements array. Here is an example of a 2D array of two rows and three columns:
Please note, that in C, you need to explicitly define the second, third, fourth and so on dimensions of an array. For example, if you want to create a three-dimensional array:
The simplest approach to pass a two-dimensional array to a function is to specify the second dimension of the array, e.g.:
The problem with the above approach is that you will only be able to pass 2D arrays with three columns to the print_2d_array function.
Please note, that you can write the print_2D_array with the alternative syntax:
which says that a is a pointer to an array of 3 integers.
A more flexible approach, is to pass the array as a pointer to the block of memory that stores the arr variable, e.g.:
The advantage of this approach is that you can pass any 2D array shape to the print_2d_array function, not only 2x3 arrays like for the first version. Another advantage is that you can use the same function to print a dynamically allocated array. A slight disadvantage is that we lose the array shape information, inside the print_2d_array function, and we can’t access the array elements using:
but the more cumbersome syntax:
Here is an example of passing a dynamically allocated array to a function:
If your compiler supports C99’s flexible array members you can conveniently group the array and dimensions information in a struct, e.g.:
The size of the Matrix2D structure is twice the size of an integer, the size of the data array, the so called flexible array member, is zero. Please note that the flexible array member should be the last element in the structure. Another observation is that flexible array members are not allowed in ISO C++, although some C++ compilers have support for this as an extension.
Here is an example of using the above structure:
If your C compiler has support for variable length arrays, you can pass the second dimension of the array as a variable to the function. Please note that, at the time of this writing, Visual Studio 2019 doesn’t have support for variable length arrays. So you won’t be able to use the next two examples with the Visual Studio 2019 C compiler. Another observation is that variable length arrays are not allowed in ISO C++.
Or, if you need to initialize the array at runtime:
Another interesting case is the so-called dynamically allocated two-dimensional array, technically this is a dynamically allocated array of pointers to dynamically allocated arrays. Here is an example of passing a 2D dynamically allocated array to a function:
In order to allocate memory for the above array we need to first allocate memory for the row pointers and next allocate memory for each row in a loop. Here is a possible implementation: