I have this two-dimensional array : array <array <int , 4> , 4> myarray
How do I declare it right in a program?
//I first include
#include <array>
then I initialize it
array<array<int,4>,4> myarray = {{
{1, 2, 3, 4},
{5, 6, 0, 8},
{9, 10, 11, 12},
{13, 14, 15, 7}
}};
I declare my functions:
void func1(array<array<int,4>,4> );
void func2();
void func3(int&, int&, array<array<int,4>,4>);
void func4(array<array<int,4>,4>);
void func5(array<array<int,4>,4>);
void func6(array<array<int,4>,4>);
write a main function:
int main() {
func1(myarray);
func2();
func3(myarray);
func4(myarray);
func5(myarray);
func6(myarray);
return EXIT_SUCCESS;
}
Then I write the functions
void func1(array<array<int,4>,4> myarray)
{//something
}
void func2();
{//something
}
void func3(int&, int&, array<array<int,4>,4> myarray);
{//something
}
void func4(array<array<int,4>,4> myarray);
{//something
}
void func5(array<array<int,4>,4> myarray);
{//something
}
void func6(array<array<int,4>,4> myarray);
{//something
}
Am I somewhere wrong ?
The point is, that the initialized array : {1, 2, 3, 4}, {5, 6, 0, 8}, {9, 10, 11, 12}, {13, 14, 15, 7}
will be input in the first func . The output of the first func will be a different 4x4 array,for example {15, 14, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {2, 1, 4, 13}
. This different array will be put as input in the second.The output of this func will be something different etc,etc....
The problem that I have is that the output of most functions is the initialized array.
Thanks for the help in advance.
The process goes like this. You initialize your array:
array<array<int,4>,4> myarray = {{
{1, 2, 3, 4},
{5, 6, 0, 8},
{9, 10, 11, 12},
{13, 14, 15, 7}
}};
Then, in main
, you call func1
. The implementation of funct1
is:
void func1(array<array<int,4>,4> myarray)
{//something
}
Since you declared func1
as void
that means that it does not return a value. array<array<int,4>,4> myarray
is type of the argument of function func1
. When you call function func1
from main
, the variable you pass from main
function will be copied into the myarray
variable of func1
(this is called pass by value), and logically, it is totally new array. If you want to change values of your function arguments you can use references or pointers. If so, implementation of your function will be:
void func1(array<array<int,4>,4> &myarray)
{//something
}
The &
symbol means that myarray
is reference of the variable you passed to the function. So if, in function func1
, you modify values of myarray
, then array in your main
function will be also modifed (because you modified the values in function func1
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.