简体   繁体   English

通过引用传递数组并修改值C ++

[英]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}. 我想编写一个函数,它接受inArray[3] = {1,2,3,4}outArray[3] ,并修改outArray[3] ,现在包含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). 您的数组大小为3,但您尝试在其中存储4个元素并访问[3]处的第四个元素(具有未定义的行为)。

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 : 使它们更大,硬编码4或使所有内容自动调整到用于初始化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. 然后,您的函数签名应指定参数的array-of- int 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... 第二个是等价的,因为当调用者传递数组参数时,它们被允许衰减为指针,甚至1)也会发生这种情况,因为只有在通过引用接受数组时才能捕获或强制执行数组维,如下例所示。 ..

  • 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). 第三个另外确保数组参数恰好有4个元素(所以突然你不能(轻松)传递说10个元素的数组并且只复制前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. 第四个接受任何大小的数组,但如果在不同大小的数组上使用不同的调用代码,它可能会创建myFunction代码的多个副本,可能会为更大的程序使用更多的内存。

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. 由于你是对元素[0][3]函数体硬编码操作,它不会调整为在元素上进一步做大的数组,但你可以选择在函数体内使用N值来计算要操作多少元素。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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