+ 1
Can anybody help me fixing this code, I am tryin to return an array from a c++ function.
7 Respuestas
+ 4
In the getData function make data static so that it remains when out of scope. Once it is out of scope it is destroyed which is where your problem lies.
static int data[] = {1, 0};
+ 2
So ChaoticDawg the problem was because I was sending an address of a destroyed variable, am I right?.
+ 2
Saeed Alqassabi Pretty much
+ 2
Thanks ChaoticDawg and rodwynnejones for the help, I appreciate it.
+ 1
You are trying to access "data" variable (which is declared inside getData() function's scope) in std::cout. Don't mix static with scoping. Static means that variable will remain the same until the program termination, but cannot be accessed out of the scope of your getData() function. Solution will be if you replace data[0] and data[1] inside your main() (line 14.) with data2[0] and [1] because getData()'s array return value is stored inside data2 which is declared inside your main()'s scope where you're accessing from.
0
Thanks *Nitrocell, it is 100% clear.