#include<cstdio>
int *f1(){
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
return arr;
}
int main(){
printf("%d", *(f1()+3));
// printf("%d", f1()[3]);
return 0;
}
The code above gives Segmentation fault
at runtime. But I don't understand why. I am creating an integer array in my f1()
function and filling it with values. Then returning the address of that array(address of the first element). So, I expect to be able to reach it from main. However, apparently, I cannot do it.