简体   繁体   中英

Pass array by reference and modify values C++

I want to write a function which takes inArray[3] = {1,2,3,4} and an outArray[3] , and modifies outArray[3] within the function to now contain values = {3,4,1,2}.

int main{
 int inArray[4] = {1,2,3,4};
 int outArray[4];

 myFunction(&inArray, &outArray);
}

void myFunction(&inArray, &outArray){
  outArray[0] = inArray[2];
  outArray[1] = inArray[3];
  outArray[2] = inArray[0];
  outArray[3] = inArray[1];
}

I'm doing something wrong here, and I don't precisely understand how to pass an array by reference and manipulate the values inside the function.

The fiunction and its call can look the following way

const size_t N = 4;

void myFunction( int ( &inArray )[N], int ( &outArray )[N] )
{
  outArray[0] = inArray[2];
  outArray[1] = inArray[3];
  outArray[2] = inArray[0];
  outArray[3] = inArray[1];
}

int main()
{
 int inArray[N] = {1,2,3,4};
 int outArray[N];

 myFunction( inArray, outArray );
}

Take into acccount that your definition of an array

int inArray[3] = {1,2,3,4};

contains a typo and will not be compiled. There must be at least like

int inArray[4] = {1,2,3,4};

or

int inArray[] = {1,2,3,4};

You arrays have size 3, but you try to store 4 elements in them and access the fourth element at [3] (which has undefined behaviour).

Make them bigger, either hardcoding 4 or making everything automatically adjust to the current length of the list of numbers you use to initialise inArray :

int inArray[] = {1,2,3,4};  // automatically sized
int outArray[sizeof inArray / sizeof *inArray];

Then, your function signature should specify the array-of- int of the arguments. There are many ways to do that:

void myFunction(const int inArray[], int outArray[])         // 1
void myFunction(const int* inArray, int* outArray)           // 2
void myFunction(const int (&inArray)[4], int (&outArray)[4]) // 3
template <size_t N>
void myFunction(const int (&inArray)[N], int (&outArray)[N]) // 4
  • The first is clearly the simplest.

  • The second is equivalent, as when a caller passes array arguments they're allowed to decay to pointers, and that happens even for 1) as the array dimension can only be captured or enforced when accepting arrays by reference, as in the following cases...

  • The third additionally ensures the array parameters have exactly 4 elements (so suddenly you can't (easily) pass say an array of 10 elements and have it copy over only the first 4).

  • The fourth accepts any sizes of array, but if used from different calling code on different sized arrays it may create multiple copies of the myFunction code, potentially using more memory for a larger program.

As you're function body hardcodes operations on elements [0] to [3] , it won't adjust to do things on elements further into larger arrays, but you have the option of using the N value inside the function body to work out how many elements to operate on.

数组总是通过引用传递,无需通过引用手动传递它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM