When I run the below program using Code::Blocks, the output is
1 2 3
8129912 -1 3
When I try it using ideone, I get an unspecified runtime error.
I would expect it to print
1 2 3
1 2 3
Can somebody explain why the string
parameter makes a difference?
int* foo() {
int a[3] {1, 2, 3};
return a;
}
int* bar(string x) {
int a[3] {1, 2, 3};
return a;
}
int main() {
int *p = foo();
cout << p[0] << " " << p[1] << " " << p[2] << endl;
int *q = bar("X");
cout << q[0] << " " << q[1] << " " << q[2] << endl;
return 0;
}