Skip to main content
Update quote to match updated text in question
Source Link
Jonathan Leffler
  • 759.5k
  • 145
  • 961
  • 1.3k

C++ solution:

"May I have any accesaccess to a local variable in a different function? If mayso, how?"

The answer is no, not after the function has ended. Local variables are destroyed at that point.

In C++ the way to deal with returning arrays is to manage them in a container like a std::array (fixed size) or a std::vector (dynamic size).

Eg:

void replaceNumberAndPrint(const std::array<int, 3>& array) { printf("%i\n", array[0]); printf("%i\n", array[1]); printf("%i\n", array[2]); } std::array<int, 3> getArray() { std::array<int, 3> myArray = {4, 65, 23}; return myArray; } 

In the second function the returned value is optimized by the compiler so you don't pay the price of actually copying the array.

C++ solution:

"May I have any acces to local variable in different function? If may, how?"

The answer is no, not after the function has ended. Local variables are destroyed at that point.

In C++ the way to deal with returning arrays is to manage them in a container like a std::array (fixed size) or a std::vector (dynamic size).

Eg:

void replaceNumberAndPrint(const std::array<int, 3>& array) { printf("%i\n", array[0]); printf("%i\n", array[1]); printf("%i\n", array[2]); } std::array<int, 3> getArray() { std::array<int, 3> myArray = {4, 65, 23}; return myArray; } 

In the second function the returned value is optimized by the compiler so you don't pay the price of actually copying the array.

C++ solution:

"May I have any access to a local variable in a different function? If so, how?"

The answer is no, not after the function has ended. Local variables are destroyed at that point.

In C++ the way to deal with returning arrays is to manage them in a container like a std::array (fixed size) or a std::vector (dynamic size).

Eg:

void replaceNumberAndPrint(const std::array<int, 3>& array) { printf("%i\n", array[0]); printf("%i\n", array[1]); printf("%i\n", array[2]); } std::array<int, 3> getArray() { std::array<int, 3> myArray = {4, 65, 23}; return myArray; } 

In the second function the returned value is optimized by the compiler so you don't pay the price of actually copying the array.

Source Link
Galik
  • 49k
  • 5
  • 85
  • 126

C++ solution:

"May I have any acces to local variable in different function? If may, how?"

The answer is no, not after the function has ended. Local variables are destroyed at that point.

In C++ the way to deal with returning arrays is to manage them in a container like a std::array (fixed size) or a std::vector (dynamic size).

Eg:

void replaceNumberAndPrint(const std::array<int, 3>& array) { printf("%i\n", array[0]); printf("%i\n", array[1]); printf("%i\n", array[2]); } std::array<int, 3> getArray() { std::array<int, 3> myArray = {4, 65, 23}; return myArray; } 

In the second function the returned value is optimized by the compiler so you don't pay the price of actually copying the array.