-2

I have an int array myArray[x][y] and I wish to pass this to a function by reference, but it seems like it either needs constant bounds or some odd pointer workaround? How do I do this properly?

user1947180
  • 39
  • 1
  • 5
  • I already looked there and it did not seem to compile when I tried the variants included there, this is a much simpler case – user1947180 Feb 12 '13 at 15:13

2 Answers2

0

An array with automatic storage duration always requires compile-time constant bounds. The fact that you can do int array[x]; where x is not a compile-time constant in GCC is merely a non-portable extension. Indeed, you cannot pass such a non-standard array by reference to a function.

You could just pass the bounds along with the array, but you're much better off using a standard container such as std::vector.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

Try:

template<int X, int Y>
void ArrayFn( int (&in)[X][Y] )
{
    in[X-1][Y-1] = 666; // or whatever
}

And call it like:

int arr[3][3];
ArrayFn( arr );
Grimm The Opiner
  • 1,778
  • 11
  • 29