C Programming

Passing a 2D Array to a Function in C

A two-dimensional array or 2D array is also called a matrix. The two-dimensional array is quite similar to tables containing rows and columns as a matrix has. You may have worked on passing a one-dimensional array to the functions. That is probably relatively easy to do. Thus, this article will demonstrate to C users how to pass a 2D array to a function. So, make sure you have a C installer configured on your Linux system to get started.

Example 01

We are starting our first illustration by creating a C file. Thus, the touch query has been used here to do so. The file would be created in the home directory of the Linux system:

$ touch test.c

To do coding, the file needs to be opened in an editor. Therefore, we have utilized the GNU Nano editor to open the file and add code to it:

$ nano test.c

After the file is launched in the Nano editor, add the standard input-output header at the start. Two constant integer type variables, “A” and “B”, are initialized after that. The variable “A” represents several rows, and “B” represents several columns. Two functions are being used in this script of C.

The execution would start from the main() function defined at the last section. It has initialized a two-dimensional array called “Array” with some values in it. The whole array is passed to a function show() as an argument within the parameters. Now, the control is assigned to the show() method. This function takes the whole integer array in its parameters.

Two “for” loops are initialized here, starting from 0 and ending on 3. You can use “A” in the row loop and “B” in the column loop. The “for” loop has been getting the elements of an array starting from its first row and first column and then, displayed on the terminal. This process continues until the last row, and the last column value of an array matrix gets displayed on the terminal. The control is assigned to the main() function again to execute the print statement.

Use the “GCC” compiler to make the code executable after error removal. The compilation has been done successfully. The execution of the code shows the array on the shell that is passed to the function “show()”.

$ gcc test.c
$ ./a.out

Example 02

Open your test.c file again in the GNU Nano editor to update it. This time, we will be getting two-dimensional array elements from the user at run time and pass this array as a value to another function.

Let’s begin with adding an input-output standard header at the top of our file. The user-defined method Display() has been declared as a prototype after that. The main() method is started with the declaration of 2D array “A” having 2 rows and 5 columns. The print statement informs a user to add array values at run time. Two “for” loops are used here, representing “i” for row and “j” for the column.

The “printf” statement shows the index of rows and columns to the user to add the array values accordingly. The scanf() method is here to get the user input at run time and save it to the respective index of an array via binding the address. After the end of both loops, the newly built array has been passed as an argument to the user-defined method “Display()” via the function call.

The Display function would be executed then, and the “printf” statement is used here to tell the user that the 2D array would be displayed now. The “for” loops are again used here to get the values from the array’s row and column and display via the “printf” statement. This 2D array has been displayed in the 2D matrix form via the “if” statement used here.

To make your code executable in the terminal, compile it first via the “GCC” compiler package. After that, execute your file with the “a.out” command. The execution starts with asking you to enter array elements at the respective index. We have added ten values one by one, followed by the “Enter” key. After the loop ends, it displays the array values in a 2D manner.

$ gcc test.c
$ ./a.out

Conclusion

This article is all about the usage and implementation of the 2D arrays in the C programming language. We have covered two simple examples to pass any 2D array to any function in the C script while working on the Linux version. We have also seen how to display the array in a 2D matrix form on the terminal console, and this was all about it. We hope you enjoyed the article, and you found the information helpful. Kindly check out our comprehensive articles at LinuxHint.com.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content