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:
The memory for the pointer array and the two-dimensional array elements is usually allocated separately using dynamic memory allocation functions, namely, malloc or calloc. A simplified function imatalloc given below allocates a regular integer matrix of size m x n to variable tmp of type int. and returns this pointer to the calling function.
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:
A one-dimensional array is a group of elements having the same datatype and same name. Individual elements are referred to using common name and unique index of the elements. The simplest form of an array is one-dimensional-array. The array itself is given name and its. C Program to print two dimensional array. Online C Array programs for computer science and information technology students pursuing BE, BTech, MCA, MTech, MCS, MSc, BCA, BSc. Find code solutions to questions for lab practicals and assignments. C program to Add and Subtract of Two One Dimensional Array elements - C programming Example. This program will add and subtract two One Dimensional Array elements in third array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x y, you would write something as follows − type arrayName x y ; Where type can be any valid C data type and arrayName will be a valid C identifier.
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.:
C Language Two Dimensional Array
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:
One Dimensional And Two Dimensional Array In C Language
If you want to learn more about C99/C11 I would recommend reading 21st Century C: C Tips from the New School by Ben Klemens:
or the classic C Bible, The C Programming Language by B.W. Kernighan, D.M. Ritchie: