How to Pass 2D Array to a Function in C++

How to Pass 2D Array to a Function in C++

Having trouble passing a 2D array in C++ without any errors? Most programmers are confused about how to select the methods between fixed-size arrays, pointers, double pointers, or STL containers such as std::vector. The incorrect method can cause segmentation faults, memory leaks, or inefficient code. In this article, let us learn about 2D arrays and how to pass the 2D arrays in C++.

Table of Contents:

What is a 2D Array?

A two-dimensional (2D) array in C++ is a collection of items in the form of rows and columns, like a matrix. For a 2D array, each of its elements is accessed by two indices, a row and a column. It can be considered an array of arrays. But with 2D arrays, it’s easy to organize the data and manipulate the data.

Why Do We Need to Pass the 2D Array to a Function?

In C++, in the case of passing the 2D array to a function, only the elements present in the array can be accessed. We can achieve this using pointers, dynamic memory allocation, and STL( standard template library) such as std::vector.

Methods to Pass the 2D Array to a Function

There are multiple methods to pass the 2D array to a function, depending on the code usage.

Method 1: Using a Fixed-Size 2D Array to Pass the 2D Array to a Function

In the C++ fixed-size 2D Array method, columns are to be specified at compile time, and rows should be specified at any time. This way, the function can access the elements with normal indexing as no extra pointer calculations are involved.

Example:

Cpp

Output: 

In the above code, the function printArray created the array of 2X3 and printed its elements in a matrix format. 

Method 2: Using a Single Pointer (int* arr) to Pass the 2D Array to a Function

In C++, the single-pointer (int *arr) method uses a single pointer to pass the 2D array as a 1D block in memory. This method allows the elements to access the index calculations like(i * cols + j). This method requires careful indexing to maintain the relevant structure. 

Example: 

Cpp

Output:

In the above code, the 2D array, as a single pointer, passes the 2D array as a 1D block in memory. in printArray(), the elements are accessed using the formula *(arr + i * cols + j) and generate the elements. 

Method 3: Using the Double Pointer (int** arr) to Pass the 2D Array to a Function

In C++, the double pointer is used to handle the 2D array dynamically. Here, each row has a separate memory block. This method requires manual memory allocation for rows and columns.

Example:

Cpp

Output: 

In this program, each row is dynamically allocated using a 2D array with a double pointer. The printArray() function prints each element using arr[i][j]; after use, the allocated memory is freed to prevent memory leaks. 

Method 4: Using std::vector to Pass the 2D Array to a Function

The method std::vector as vector<vector<int>> is used to create a dynamic 2D array. This method provides automatic memory management with a built-in function vector that handles the 2D data efficiently.

Example:

Cpp

Output: 

This program utilizes the std::<vector> to construct a 2D array that handles memory automatically. The printArray() function prints the elements in a row-wise.

Method 5: Passing a 2D Array with Rows and Columns to a Function 

In C++, we pass a 2D array by mentioning the number of columns specifically, where passing several rows as a parameter.

Example:

Cpp

Output: 

The printArray function accepts a 2D array with a fixed number of columns (int arr[][3]), but the number of rows is passed as a parameter. It then applies nested loops to traverse the array and print its elements.

Method 6: Passing a 2D Array with Rows and Columns Declared Globally to a Function 

A globally declared 2D array can be called by any function without necessarily passing it as a parameter. This makes function calls easier as the array is available across the program.

Example: 

Cpp

Output:

In the above code, the 2D array is assigned as global, i.e., it is accessible to all functions. The printArray prints the output without requiring any parameters.

Conclusion

There are several ways in C++ to pass and manipulate 2D arrays. Fixed-size must know column size. With double pointers (int* arr), you see just a block of 1D, and you have to index, which is done manually. Double pointers use (int** arr); this allows dynamic allocation, but memory management is getting a little harder. Using std::vector gives you the most flexibility, built-in memory management, and easy resizing.

FAQs on How to Pass a 2D Array to a Function in C++

1. Is it possible to pass a fixed size 2D array to a function?

Yes, you can pass a fixed size 2D array to a function by specifying the number of rows and columns when declaring the function parameter.

2. Is it possible to pass a 2D array with a single pointer (int* arr)?

Yes, when passing the 2D array with the single pointer(int* arr), you need to manually calculate indexes via (i * cols + j) because the array is seen as a 1D block in memory.

3. How do I pass a pointer as an argument (int** arr) for a dynamic 2D array?

You can allocate the rows and cols as separate memory and pass it as void func(int** arr, int rows, int cols).

4. What is the most efficient way to even pass a 2D array?

The simplest is to use std::vector <std::vector<int>>, which manages the memory for you and can be resized easily.

5. Is it possible to use std::array to pass a 2D array?

Yes, it is possible, but it requires a fixed size at compile time.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner