Passing 2d array to function in C with Example Program

Spread the love
Passing-2d-array-to-function-in-C-with-Example-Program

Passing 2d Array to Function in C Program Description:

In our previous article, We have looked at How to Read and Print the 2D Array elements in C language, In today’s article, We will look at Passing 2d Array to Function in C programming language with an example program. The Program should take an input matrix(2d array) from the user and Define two functions to read the 2d array elements and display the 2d array.

Example Input and Output:

Input:

Output:

If we remove the array prompt ( A[i][j]) during the Input phase. Then the program no longer shows the element index like A[0][0], A[0][1], etc. Make sure to enter the correct number of elements 🐧

Input:

Output:

Prerequisites:

It is recommended to go through the Arrays, Functions in C language, and 2D Arrays in C to better understand the following program.

Passing 2d Array to Function in C Program Explanation:

Let’s look at the step-by-step explanation of Passing a 2d Array to Function in a C Program.

  1. Create two Constants named MAXROWS and MAXCOLUMNS and initialize them with 100. The MAXROWS and MAXCOLUMNS constants hold the maximum size of the rows and columns of a 2D Array. If you want to use large arrays, Please change this number to the desired value.
  2. Declare the input array A[MAXROWS][MAXCOLUMNS] with MAXROWS and MAXCOLUMNS.
  3. Prompt the user to provide the desired row size and column size. and store the row size in rows variable and column size in columns variable.
  4. Check for array bounds using the (rows >= MAXROWS || rows <= 0 || columns >= MAXCOLUMNS || columns <= 0) condition, And display an error message if the rows or columnare beyond the present limits.
  5. Define Two Functions – readMatrix() function and displayMatrix() function.
  6. The readMatrix() function used to read the input from the user and update the 2D Array or Matrix( A) Elements
    • void readMatrix(int rows, int columns, int A[rows][columns])
    • The readMatrix() function takes three formal arguments, which are rows size, columns size, and A[][] 2D Array.
    • This function uses two For loops to iterate over the given matrix A and updates its elements by using the scanf function – scanf(“%d”, &A[i][j]);
    • readMatrix() function doesn’t return anything back to the caller. So return type is void.
    • As the arrays are passed by the address/reference, All the changes made to the 2D Array in the readMatrix() function will be visible in the Caller Function (i.e main() function).
  7. The displayMatrix() function is used to print the 2D Array or Matrix elements on the Console.
    • void displayMatrix(int rows, int columns, int A[rows][columns])
    • Similar to the readMatrix() function, The displayMatrix() function also takes three formal arguments. i.e rows, columns, and A[][] (2D Array)
    • The displayMatrix() function uses two For loops to iterate over the A matrix and prints the elements of the matrix. – printf("%d ", A[i][j]);
  8. From the main() function, Call the readMatrix() function and pass the rows, columns, and 2D array( A) as the parameter – readMatrix(rows, columns, A);
  9. Similarly, Make another call to displayMatrix() function to display the 2D array on the console – displayMatrix(rows, columns, A);

Program to understand Passing 2d Array to Function in C:

Here is the program to demonstrate the Passing of 2D Array to Function in the C Programming language

To better understand the program, We have added a lot of comments to the program. Please go through the comments if you face any doubts.

Program Output:

Let’s compile and Run the program.

Compile the Program.

$ gcc passing-2darray-func.c

Run the Program

Test 1: Normal cases:

Passing-2d-Array-to-Function-in-C-Example-program-output

As we can see from the above output, The program is properly reading and printing the 2D Array or Matrix.

Test 2: When the user enters Invalid Size:

As excepted, When the user enters an array size that is out of the present limit (1 to 100), The program is displaying the error message – Invalid Row or Column, Please Try again.

Remove the Prompt Message while reading the 2D Array Input:

Let’s make a one-line change to the above program to remove the info message in the readMatrix() function. So that we no longer get the element index details during the array input.

We commented the printf at line 31 in the readMatrix() function.

Program Output:

Compile and Run the Program.

Passing-2d-Array-to-Function-in-C-Example-program-2

As we can see from the above output, The program is no longer showing the array index during the input phase.

Related Array Programs:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] have looked at the Reading and Printing Matrices and How to Pass Matrices to functions programs in earlier articles, In today’s article, We will look at the Addition of Two Matrices in […]

  2. […] C Program to demonstrate How to Pass 2D Array to Functions […]

  3. […] C Program to demonstrate How to Pass 2D Array to Functions […]

Leave a Reply