Multidimensional Array Pointer

Hi,

I have a number multidimensional arrays of different sizes. I would like to create a pointer to these arrays and pass that down in a function. For instance:

int test2[][2] = {{1,2},{3,4},{5,6}};
int (*__w64 test)[][2] = &test2;

The array test2 in this case is initialized as a 3x2 array of int. I wish to establish a pointer to a variable size array called test. In this case test is assigned the array pointer of test2. I would like to be able to assign an array to test of any nx2 array size.

When I compile this in Visual Studio 2005 I get an error at line 2:
cannot convert from 'int (*__w64 )[3][2]' to 'int (*__w64 )[][2]

I got this code from a source that compiled on a different compiler so I'm assuming that is a Visual Studio thing. I tried Visual Studio 2012 with no difference.
Thanks,
First of all, variable-length arrays (and pointers to variable-length arrays) are a C99 language feature, not yet supported by Visual Studio at all (they didn't support any C past C89 until very recently, but started adding C99 and C11 features in 2013 and 2015, but didn't get to VLAs yet). Good news is that variable-length arrays do not appear anywhere in your program.

The type of the pointer test is int(*)[][2], pointer to fixed-size array of unknown bound of arrays of 2 int

C, but not C++, allows initialization (and assignment) of a pointer of type int(*)[][2] from a pointer of type int(*)[3][2] and vice versa. You can get your program to compile in Visual Studio if you engage the C compiler, not the C++ compiler: http://rextester.com/VDPK45252

Note that pointers to arrays of unknown bound have limited use, for example you can't form expressions "test++" or even "test[0]". About the only thing you can do is dereference it (so (*test)[0][0] works)

If you're actually using C++, as the error message "cannot convert..." suggests, then use C++ containers, such as std::vector<pair<int, int>>

PS: this conversion may become allowed in C++ in future, see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4325.html#118
Last edited on
Thanks for the great information. After I switched to build in C I was able to compile. I still get a warning about the different array subscripts but as long as it works it doesn't matter for my application.
Topic archived. No new replies allowed.