Returns an array in C ++

Suppose I have an array

int arr[] = {...}; arr = function(arr); 

I have a function like

  int& function(int arr[]) { //rest of the code return arr; } 

What mistake am I making here?

+7
c ++ arrays
source share
11 answers
  int& function(int arr[]) 

In this function, arr is a pointer, and there is no way to return it back to an array. This is the same as

 int& function(int* arr) 

 int arr[] = {...}; arr = function(arr); 

Assuming the function was able to return a reference to the array, this still will not work. You cannot assign an array. At best, you can bind the result to an "X array ints reference" (using typedef, because the syntax will be very ugly otherwise):

 typedef int ten_ints[10]; ten_ints& foo(ten_ints& arr) { //.. return arr; } int main() { int arr[10]; ten_ints& arr_ref = foo(arr); } 

However, it is completely unclear what the code should achieve in your question. Any changes you make to the array in the function will be visible to the caller, so there is no reason to try to return the array or assign it back to the original array.


If you need a fixed-size array that you can assign and pass by value (with copied content), there is std::tr1::array (or boost::array ). std::vector also an option, but it is a dynamic array, so it is not an exact equivalent.

+7
source share

According to others, there are better options, such as STL containers, and the most interesting question is why you need to return the array to a function that already has it in the scope.

At the same time, you cannot pass or return arrays by value and should avoid decomposing the array into a pointer (see, for example, here ), or using pointers or references to it:

 int (&f(int (&arr)[3]))[3] { return arr; } 

If you do not want to set the size hard, you can use the template functions:

 template<size_t n> int (&f(int (&arr)[n]))[n] { return arr; } 
+6
source share

The semantics of passing and returning an array can be quite confusing. Use std :: vector instead.

+4
source share

You are returning an int reference, not an array reference.

Do you want to:

 (int*)& function(int arr[]) { /// rest of the code return arr; } 

However, as others have said, this is not good practice.

+3
source share

The following works do not seem messy or incomprehensible!

 int* returnarray(int a[]) { for (unsigned int i = 0; i < 3; i++) { a[i] *= 2; } return a; } int main(int argc, char* argv[]) { int arr[3] = {1,2,3}; int* p = returnarray(arr); for (unsigned int i = 0; i < 3; i++) { cout << "p[" << i << "] = " << p[i] << endl; } cin.get(); return 0; } 
+3
source share
  • An argument as an argument is actually a pointer. Arrays are automatically "thrown" by pointers if they are specified as an argument
  • You cannot assign an array to another array in C / C ++. You will need to use memcpy or loop to copy the values.
  • If the function changes the arr argument, it actually changes the value of the arr variable specified by the caller. Again, because the array is actually a pointer. Thus, in the caller, arr is assigned to arr, which is useless here.
+1
source share

I think your best bet is to return it as a pointer to an array. Here is an example:

 { ... int newLength = 0; int* arr2 = ChangeArray(arr, length, newLength); ... delete[] arr2; } int* ChangeArray(int arr[], int length, int& newLength) { // reversing the number - just an example int* newArr = new int[length]; for(int i = 0; i < length; i++) { newArr[i] = arr[length-i-1]; newLength++; } return newArr; } 
+1
source share

Well, arr is compatible with int* , not int& . Your code will not compile. Perhaps you wanted return arr[0] ?

0
source share

You cannot assign an array to another array in C ++. Consider using the STL vector: http://www.cplusplus.com/reference/stl/vector/

0
source share

Use std :: vector like this.

 std::vector<int> function(const std::vector<int>& arr) { return arr; } 

Array like

 int arr[] = {...}; 

it is not advisable to return from a function, because it is not able to copy itself.

0
source share

if not necessary, do not use it. you can just pass an array reference, for example:

 void foo(std::vector<int> &v) { for (int i = 0; i < 10; ++ i) { v.push_back(i); } } 

if you use:

 std::vector<int> foo() { std::vector<int> v; for (int i = 0; i < 10; ++ i) v.push_back(i); return v; } 

there will be a process of copying a container, the cost will be expensive.

FYI: NRVO can eliminate cost

0
source share

All Articles