C++ Return 2D Array From Function
Last Updated :
22 Dec, 2022
An array is the collection of similar data-type stored in continuous memory. And when we are storing an array inside an array it is called 2 D array or 2-dimensional array. To know more about arrays refer to the article Array in C++.
When there is a need to return a 2D array from a function it is always hard to find the way to do that, and the reason why it is hard because when we return an array local to some function it only returns the address of that array and once the function returning address is completed and we have reached to the place where we had called it. Then operating system will destroy all the memory which was created in that function so in that case the array that we get from that is also destroyed and we are holding the address of the illegal memory area.
So, in this article, we will see how to deal with this problem.
Methods to Return 2D array From function
here, we will see returning of a 2D array from the function using the below methods:
- Using Dynamic Array
- Using Static Keyword
- Using Struct technique
1. Return 2D Array from Function in C++ Using Dynamic Array
Dynamic Array helps us to store arrays address using Pointer. So, we can use a dynamic array to use pointer-to-pointer notation to dynamically allocate and return an array. To know more about the dynamic array refer to Dynamic array in C++.
Below is the implementation of the above approach
C++
// C++ code for returning 2D array
// from function using Dynamic array
#include <iostream>
using namespace std;
const int N = 3;
// function to display array
void printArray(int** arr)
{
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cout << arr[i][j]<<" ";
}
cout << endl;
}
}
// function to initialize and returning array
int** getArray()
{
int** arr = new int*[N];
for (int i = 0; i < N; ++i) {
arr[i] = new int[N];
for (int j = 0; j < N; ++j) {
arr[i][j] = i + j;
}
}
return arr;
}
// Driver Code
int main()
{
int** arr;
arr = getArray();
printArray(arr);
return 0;
}
2. Return 2D Array from Function in C++ Using Static Keyword
The static keyword is used so to make the memory of the element static which means even when it is passed on the function it used the same memory element rather than making a copy of the element. To know more about static keywords refer to static keyword in C++.
Below is the implementation of the above approach
C++
// C++ code for returning 2D array
// from function using static keyword
#include <iostream>
using namespace std;
const int N = 3;
// function for display array
void printArray(int arr[][N])
{
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cout << arr[i][j]<<" ";
}
cout << endl;
}
}
// function to initialize and returning array
int (*(getArray)())[N]
{
static int arr[N][N]
= { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };
return arr;
}
// Driver code
int main()
{
int(*arr)[N];
arr = getArray();
printArray(arr);
return 0;
}
3. Return 2D Array from Function in C++ Using Struct technique
Struct is the user-defined data type that can store multiple members bound together and acts like a single unit. So, we can use a struct having an array inside it as a unit to perform any operation and return it as a unit.
Below is the implementation of the above approach
C++
// C++ code for returning 2D array
// from function using static keyword
#include <iostream>
using namespace std;
const int N = 3;
// structure of array
struct ArrStruct {
int arr[N][N];
};
// function for display array
void printArray(ArrStruct var)
{
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cout << var.arr[i][j] << " ";
}
cout << endl;
}
}
// function to initialize and returning array
ArrStruct getArray()
{
ArrStruct var;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
var.arr[i][j] = i + j;
}
}
return var;
}
// Driver code
int main()
{
ArrStruct arr;
arr = getArray();
printArray(arr);
return 0;
}
Similar Reads
Return Array From a Function in Objective-C An array is a form of data structure that has a homogenous collection of data in a fixed size. In a nutshell, an array is a group of variables of the same type. For instance, integers cannot be stored in an array of string types. The lowest address of the first element of the array is 0, while the h
6 min read
How to Return a Local Array From a C++ Function? Here, we will build a C++ program to return a local array from a function. And will come across the right way of returning an array from a function using 3 approaches i.e. Using Dynamically Allocated ArrayUsing Static Array Using Struct C++ // C++ Program to Return a Local // Array from a function W
3 min read
array get() function in C++ STL The array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container. Syntax: get(array_name) Parameters: The function accepts two mandatory parameters which are described below. i - position of an element in the array, with 0 as the position of the
2 min read
Pass Array to Functions in C Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe
3 min read
Pass Array to Functions in C++ In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code. In C++, an array can be passed in a function using a poi
5 min read
How to return local variables from a function in C++ In C++, returning a local variable from a function may result in an error due to the deallocation of variable memory after some value is returned from the function. But there are some ways using which we can safely return the local variables or the address of local variables and manipulate the data
4 min read
Return Pointer From Functions in Objective-C A pointer is like a variable or constant that stores the address of another variable. It must be declared before the address value of a variable is to be stored in it. The main advantage of using a pointer is that it frees up the program memory and provides direct access to memory locations. It also
5 min read
Arrays and Strings in C++ Arrays An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar types of elements as in the data type must be the same for all elements. They can be used to store the collection
5 min read
array::front() and array::back() in C++ STL Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::front() This function is used to reference the first element of the array container. This function can be
3 min read
Changing Array Inside Function in C When declaring an array the array name is always called the pointer because in an array name the address of the 0th block(base address) is stored. To change an array in the function we have to pass an array in the function. For this, an array_name is passed to the function in the form of an actual a
2 min read