How to Return a Local Array From a C++ Function? Last Updated : 14 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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 While // violating some rules #include <iostream> using namespace std; int* fun() { int arr[100]; // Some operations on arr[] arr[0] = 10; arr[1] = 20; return arr; } int main() { int* ptr = fun(); cout << ptr[0] << " " << ptr[1]; return 0; } Warning: In function 'int* fun()': 6:8: warning: address of local variable 'arr' returned [-Wreturn-local-addr] int arr[100]; ^ The above program is WRONG. It may produce values of 10 or 20 as output or may produce garbage values or may crash. The problem is, that we return the address of a local variable which is not advised as local variables may not exist in memory after the function call is over. Following are some correct ways of returning an array 1. Using Dynamically Allocated Array Dynamically allocated memory (allocated using new or malloc()) remains there until we delete it using the delete or free(). So we can create a dynamically allocated array and we can delete it once we come out of the function. Example: C++ // C++ Program to Demonstrating returning of a local array // from a function Using Dynamically Allocated Array #include <iostream> using namespace std; int* fun() { int* arr = new int[100]; // Some operations on arr[] arr[0] = 10; arr[1] = 20; return arr; } int main() { int* ptr = fun(); cout << ptr[0] << " " << ptr[1]; // allocated memory must be deleted delete[] ptr; return 0; } Output10 202. Using static Array The lifetime of a static variable is throughout the program. So we can always create a local static array and return it. Example: C++ // C++ Program to Demonstrating // returning of a local array from // a function Using Static Array #include <iostream> using namespace std; int* fun() { static int arr[100]; // Some operations on arr[] arr[0] = 10; arr[1] = 20; return arr; } int main() { int* ptr = fun(); cout << ptr[0] << " " << ptr[1]; return 0; } Output10 203. Using struct We can wrap the array in a structure/class and return an instance of the struct/class. The reason for this work is, that the array of members of structures is deeply copied. In the below program deep copy happens when we returned instance is copied in main. Example: C++ // C++ Program to Demonstrating returning of a local // array from a function using Struct #include <iostream> using namespace std; struct arrWrap { int arr[100]; }; struct arrWrap fun() { struct arrWrap x; x.arr[0] = 10; x.arr[1] = 20; return x; } int main() { struct arrWrap x = fun(); cout << x.arr[0] << " " << x.arr[1]; return 0; } Output10 20 Comment More infoAdvertise with us Next Article How to Return a Local Array From a C++ Function? kartik Follow Improve Article Tags : C++ cpp-array CPP-Functions Practice Tags : CPP Similar Reads 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 C++ Return 2D Array From Function 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 al 4 min read How to pass or return a structure to/from a Function in C/C++? A structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. How to pass structure as an argument to the functions? Passing of structure to the function can be done in two ways: By passing all the el 3 min read Return From Void Functions in C++ Void functions are known as Non-Value Returning functions. They are "void" due to the fact that they are not supposed to return values. True, but not completely. We cannot return values but there is something we can surely return from void functions. Void functions do not have a return type, but the 2 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++ 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 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 array at() function in C++ STL The array::at() is a built-in function in C++ STL which returns a reference to the element present at location i in given array. Syntax: array_name.at(i) Parameters: The function accepts a single mandatory parameter i which specifies the location. Return value: The function returns an element presen 2 min read How to print size of array parameter in C++? How to compute the size of an array CPP? C++ // A C++ program to show that it is wrong to // compute size of an array parameter in a function #include <iostream> using namespace std; void findSize(int arr[]) { cout << sizeof(arr) << endl; } int main() { int a[10]; cout << si 3 min read Pointer to an Array in C++ Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or 6 min read Like