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};
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.
source share