Pass array by reference and change C ++ values

I want to write a function that takes inArray[3] = {1,2,3,4} and outArray[3] , and modifies outArray[3] inside the function to now contain the 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 am doing something wrong here, and I don’t quite understand how to pass an array by reference and manipulate the values ​​inside the function.

+6
source share
3 answers

A function and its call may look as follows.

 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 ); } 

Bear in mind that your array definition

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

contains a typo and will not compile. Must be at least

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

or

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

Your arrays are 3 in size, but you are trying to store 4 elements in them and access the fourth element in [3] (which has undefined behavior).

Make them large, either hardcoding 4, or make everything automatically set to the current length of the list of numbers that you use to initialize inArray :

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

Then your function signature must indicate the argument array-of- int . There are many ways to do this:

 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 obviously the simplest.

  • The second is equivalent, since when the caller passes the arguments of the array, they are allowed to decompose into pointers, and this happens even for 1), since the measurement of the array can be fixed or forced only when accepting arrays by reference, as in the following cases ...

  • The third additionally guarantees that the parameters of the array have exactly 4 elements (so you cannot (easily) pass say an array of 10 elements and copy it only to the first 4).

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

Since you perform hardcodes operations with functions [0] to [3] , they will not be configured to do something further with elements in larger arrays, but you can use the N value inside to determine how many elements to use.

+1
source

The array is always passed by reference, you do not need to pass it by reference manually.

0
source

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


All Articles