C++ returning 2D array from a function

C++ returning 2D array from a function

 An array is a collection of similar types of data stored in contiguous memory. And when we store an array within an array, it is called a 2D array or 2D array. To learn more about arrays, see Arrays in C++.

notfound

When there is a need to return a 2D array from a function, it is always difficult to find a way to do it, and the reason why it is difficult is that when we return an array that is local to some function, it only returns the address of that array. and once the function returns the address is complete and we have reached the place where we called it. The operating system will then destroy all the memory that was created in this function, so in this case the array we get from this is also destroyed and we store the address of the invalid memory area.

So, in this article, we will see how to deal with this problem.

Methods for returning a 2D array from a function

here we will see the return of a 2D array from a function using the following methods:

  • Using a dynamic array
  • Using the static keyword
  • Using the Struct Technique

1. Returning a 2D array from a function in C++ using a dynamic array

A dynamic array helps us store the addresses of arrays using a pointer. So we can use a dynamic array to use pointer to pointer notation to dynamically allocate and return an array. To learn more about dynamic array, refer to Dynamic Array in C++.

Following is the implementation of the above approach

// 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;

}

Conclusion

0 1 2

1 2 3

2 3 4

2. Returning a 2D array from a function in C++ using the static keyword

The static keyword is used to make an element's memory static, which means that even when it is passed to a function, it uses the same memory element rather than creating a copy of the element. To learn more about static keywords, see the static keyword in C++.

Following is the implementation of the above approach

        // 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;

}

Conclusion

0 1 2 

3 4 5 

6 7 8

3. Returning a 2D array from a function in C++ using the Struct technique

Struct is a user-defined data type that can store multiple elements linked together and acts as a single entity. So, we can use a structure with an array inside as a unit to perform any operation and return it as a unit.

Following is the implementation of the above approach

// 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;

}

Conclusion

0 1 2

1 2 3 

2 3 4

Please leave your comment to encourage us

Previous Post Next Post