Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 671
Rohit Avatar asked Mar 17 '15 07:03

Rohit


People also ask

Can you pass an array by reference in C?

An array can be passed to functions in C using pointers by passing reference to the base address of the array, and similarly, a multidimensional array can also be passed to functions in C.

How do you change the value of a specific element in an array in C?

you can change the integers pointed at directly via the * operator, so *array = 99; will change the first element, *(array+1) = 98; the second and so on. you can also, more naturally use the [] operator. so in your function array[0] = 99; will actually change the original array. Save this answer.

How do you modify a value in an array?

To change the value of all elements in an array:Use the forEach() method to iterate over the array. The method takes a function that gets invoked with the array element, its index and the array itself. Use the index of the current iteration to change the corresponding array element.

What is array in C How can you reference it?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To create an array, define the data type (like int ) and specify the name of the array followed by square brackets [].


1 Answers

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.

like image 126
Tony Delroy Avatar answered Oct 06 '22 03:10

Tony Delroy