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};
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments