when declaring the two dimensional array
int random[height][width];
and then using it in a function
void populate(int random[height][width], int x, int y)
gives the error variable-size type declared outside of any function.
I know I’m doing something wrong, and that its something small. I just have a bad memory…
I’m going to step up right now and tell you that multidimensional arrays are not worth the brain effort in C or C++. You’re much better off using single-dimensional arrays (or, better yet, standard containers) and writing an indexing function:
Now for your problem. C++ does not support C99 variable-length arrays. The compiler must know, at compile time, the size of the array. The following, for example, won’t work.
If
dimwereconst, it would work because the compiler would be able to tell exactly how widearshould be (because the value ofdimwouldn’t change). This is probably the problem you’re encountering.If you want to be able to change the size at compile-time, you’ll need to do something more difficult, like write a templated reference. You can’t use a pointer for multidimensional arrays because of the way they are laid out in C/C++. A templated example might look like the following aberration:
This is ugly.
For run-time, you’ll need to use
newto allocate data, or use a container type.