You cannot return an inline array in C ++.
If you're new to C ++ and confused about pointers, you really don't want to use arrays (at least not inline arrays). Use std::vector<int> instead, or if you only have a certain number of elements and want to express it (and really need better performance), use boost::array<int, N> . (or even std::array<int, N> if you are programming in C++11 (if you do not know if you are programming in C++11 ), most likely you are not doing this). For instance:
std::vector<int> myfunction(const std::vector<int>& my_array) { std::vector<int> f_array; for(int i = 0; i < my_array.size(); ++i) f_array.push_back(my_array[i]); return f_array; } boost::array<int, 2> myfunction(const boost::array<int, 2>& my_array) { boost::array<int, 2> f_array; f_array[0] = my_array[0]; f_array[1] = my_array[1]; return f_array; }
Then you can make the copy code simpler (find the constructors and member functions of any class that you decide to use, as well as the STL algorithms). Example:
std::vector<int> myfunction(const std::vector<int>& my_array) { std::vector<int> f_array(m_array); ... return f_array; }
There is an error as another point in your code: you define my_array as int my_array[1] , which means its array with one element, but you get access to two elements ( my_array[0] and my_array[1] ), access to my_array[1] is outside ( int foo[N] takes place for elements of N , starting at index 0 and going to index N-1 ). I assume that you really mean int my_array[2] .
source share