function that return array c++ code example
Example 1: return array from function c++
#include <iostream>
using namespace std;
int* fun()
{
int* arr = new int[100];
arr[0] = 10;
arr[1] = 20;
return arr;
}
int main()
{
int* ptr = fun();
cout << ptr[0] << " " << ptr[1];
return 0;
}
Example 2: cpp return array
int * fillarr(int arr[], int length){
for (int i = 0; i < length; ++i){
}
return arr;
}
int main(){
int arr[5];
int *arr2;
arr2 = fillarr(arr, 5);
}
Example 3: return array of string in function c++
string* getNames() {
string* names = new string[3];
names[0] = "Simon";
names[1] = "Peter";
names[2] = "Dave";
return names;
}
Example 4: return array of string in function c++
delete[] names;