C ++ returned array from function

I need to read my function in the array, extract the data and then return the array from this function.

The array will contain only 2 values.

This is what I want to do in concept:

int myfunction(int my_array[1]) { int f_array[1]; f_array[0] = my_array[0]; f_array[1] = my_array[1]; // modify f_array some more return f_array; } 

I read about pointers, etc., but was very confused and would be a really basic example of how best to approach this!

Thanks!

+4
source share
8 answers

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] .

+9
source
 std::array<int,2> myfunction(int * my_array) { std::array<int,2> f_array; f_array[0] = my_array[0]; f_array[1] = my_array[1]; // modify f_array some more return f_array; } 

Please note that as parameter int my_array[1] exactly matches int my_array[1000] , int my_array[] or int * my_array . This is just a pointer, and the size value is pointless. This does not apply to regular array declarations, just parameters.

To ensure that only arrays of size 2 are passed to you, you can take the array by reference:

 std::array<int,2> myfunction(int (&my_array)[2]) 
+4
source

You cannot return an array from a function, but you can add a second argument to the "out" array:

 void foo(int array_in[], int array_out[], int array_size) { for (int i = 0; i < array_size; i++) array_out[i] = array_in[i]; } 
+1
source

If you have strictly 2 elements in an array, I suggest std::pair

 typedef std::pair<int,int> int_pair; int_pair myfunction(int_pair ip) { ip.first *= 0.12; ip.second -= 355; return ip; } 
+1
source

You cannot return an array in C or C ++.

When you work in C ++, use the container class instead of the raw array. A std::vector or boost::array would be good options.

0
source

An array with two values ​​must be declared as int my_array[2] . Anyway, you cannot copy arrays by value. You said that you will only have two objects in the object. Therefore, I suggest that you use pair<int,int> instead.

 using namespace std; pair<int,int> myfunction(pair<int,int> my_array) { pair<int,int> f_array; f_array.first = my_array.first; f_array.second = my_array.second; // modify f_array some more return f_array; } 
0
source

If you need only two values, I would look at std :: pair , in the end what it is there for. It also has the correct copy (move in C ++ 11) to make this work correctly.

0
source

C ++ does not allow returning the entire array as an argument to a function. However, you can return a pointer to an array by specifying the name of the array without an index.

To return a one-dimensional array from a function, you need to declare a function that returns a pointer, as shown below:

 int * myfunc(){ static int arr[size_of_array]; /* code for manipulation of arr */ return arr; } 

Variables declared inside the function body are destroyed as soon as we leave the function block. Therefore, we need to declare int arr[size_of_arr] as static . This will allocate memory for the int arr[size_of_arr] on the heap and ensure that it will not be destroyed as soon as we leave the function block.

The method of calling myfunc() described below:

 int *myarray; myarray=myfunc(); 

Now the array returned by myfunc() gets storage in myarray

0
source

Source: https://habr.com/ru/post/1389496/


All Articles