how to return array from a function c++ code example
Example 1: 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 2: return an array in c++
#include <iostream>
#include <ctime>
using namespace std;
int * getRandom( ) {
static int r[10];
srand( (unsigned)time( NULL ) );
for (int i = 0; i < 10; ++i) {
r[i] = rand();
cout << r[i] << endl;
}
return r;
}
int main () {
int *p;
p = getRandom();
for ( int i = 0; i < 10; i++ ) {
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
return 0;
}