I wanted to pass an array by reference in a function from another function. Here is my code below
// Objectives:
// 1. Passing an array to a method
// 2. Returning arrays from a method
#include <iostream>
using namespace std;
int getArray(int* arr) {
for (int i=0; i< 11; i++) {
cout << arr[i] << endl;
}
return 0;
}
int* returnArray() {
int arr [11] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21};
return arr;
}
int main() {
getArray(returnArray());
return 0;
}
Here is my output:
get_return_array.cpp:17:12: warning: address of stack memory associated with local variable 'arr' returned
[-Wreturn-stack-address]
return arr;
^~~
1 warning generated.
1
32767
170167402
1
9
11
2051556088
32767
17
9
1436251616
Why values of array are some garbage values and what does the warning signifies?
Marked as duplicate, why??: I have shown usage of static
with explanation while the others' answer haven't.