Passing multi-D array to function w/o referencing it

How to do it? I've tried:

data-type function(data-type arrayVar[][n])
and
data-type function(data-type arrayVar[n][n])

But both are references. Is there no other way than to use vectors? The 2D array that need to be copied is same-sized and full through the whole program, so I'd rather stick to classic arrays.

Thanks in advance!
These really aren't references. They are arrays (pointers, technically) who are being inserted as parameters. Post some concrete code to clarify the trouble you're having.
When I say references I mean that they are not copies of the array itself. At least they act like they're not.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void test(int some_array[4][4]) {
some_array[0][0] = 666;
}

int main() {
int some_array[4][4] = {{1, 2, 3, 4}
{5, 6....}
...
};

test(some_array);
cout << some_array[0][0]; //outputs 666 instead of 1;
return 0;
}


(wrote this here, so apologies for ugly code formatting)
You can't. Arrays cannot be passed by value in C++.

If you want to do this, you must encase the array in some other container (like a struct or a vector or something).

But that's a bad idea. There's no reason to create a copy of the array, as that's computationally expensive and a big waste.

Just pass the array by pointer/reference like you're doing and just don't have the function change it. To ensure the function doesn't change it, pass by const pointer/reference:

1
2
void test(const int some_array[4][4]) {
  some_array[0][0] = 666;  // now this gives you a compiler error 
Thanks. I already thought about using a struct, but since I'm writing knight's tour (the chess problem), I'd like to keep it as simple and basic as possible.
The code is already finished actually, but the problem are dead ends when the program has to return (function recursion). Values get changed in all functions, while they should stay unique for each function. And I know it's a bit insane to copy 8x8 array up to 64-times, but it's the most simple way that I could think of.
The thing is that every square has a number that tells the program how many moves are possible from that square. So when the program has to return, all those numbers have to be reset to the state where another "tour" is possible.

What do you suggest? Would copying 1-dimensional 64-element vector (or string) be any less terrible? Is there actually any major difference between 1D and multi-D array (performance and allocation-wise)?
Topic archived. No new replies allowed.