-
Returning Entire Arrays
I was wondering if I was doing this correctly. Does this line of code:
Code:
main()
{
siPlayerGrid[8][16][5] = setter(siPlayerGrid, siCompGrid, i, j);
}
setter(short siPlayerGrid, short siCompGrid, short i, short j)
{
return siPlayerGrid[8][16][5];
}
Correctly return the whole array?
Thanks in advance.
-
Sorry, I meant this code:
Code:
main()
{
siPlayerGrid[8][16][5] = setter(siPlayerGrid, siCompGrid, i, j);
}
setter(short siPlayerGrid[8][16][5], short siCompGrid[8][16][5], short i, short j)
{
return siPlayerGrid[8][16][5];
}
-
Itīs difficult to say. I canīt see anything wrong in your code, but nethier can I see anything usefull...
-
I'm just trying to ensure that it is returning the entire array, rather than just the values at those elements, (though the subsets are [0-7][[0-15][0-4]).
-
When called, it passes the values, when returning it passes the values!
Itīs like this:
Code:
function(type passedbyvalue,type& passedbyreference);
The passedbyreference variable shouldnīt be returned though!
-
Using & would pass it as a pointer, how would I pass the arrays as a reference so I don't have to return them?
-
No, my friend, using & passes it as reference.
Using * passes it as pointer.
Try out this piece of code yourself:
Code:
#include <iostream>
using namespace std;
void add1(int& passedbyreference){
passedbyreference++;
}
int main(int argc, char** argv){
int i = 0;
add1(i);
cout << i << endl;
}
-
>how would I pass the arrays as a reference so I don't have to return them?
actually, arrays are always passed by reference, doesnt matter if you use & or not.
-
Really!? Great, this makes my program more efficient since it doesn't need so many memory allocations when I send arrays to functions!
Thanks!