http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51352
Bug #: 51352
Summary: g++ can't pass variable-length array as a function
argument
Classification: Unclassified
Product: gcc
Version: 4.6.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: [email protected]
ReportedBy: [email protected]
According to http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html, gcc
supports variable-length arrays as an extension when compiling C++ code. An
example is given on that page of how to pass such an array as an argument to a
function. However, I haven't been able to get it to work. As an example, the
simple program
#include <iostream>
#include <cstdlib>
void fill_array(int dim, int array[dim][dim]);
int main(int argc, char *const argv[]) {
int dim = atoi(argv[1]);
int array[dim][dim];
fill_array(dim, array);
for(int i = 0; i < dim; ++i) {
for(int j = 0; j < dim; ++j) {
std::cout << array[i][j] << "\n";
}
}
}
void fill_array(int dim, int array[dim][dim]) {
int counter = 0;
for(int i = 0; i < dim; ++i) {
for(int j = 0; j < dim; ++j) {
array[i][j] = counter++;
}
}
}
won't compile under g++ version 4.6.1 (I think that's the version I was
running), despite what that page claims, instead giving errors claiming that
the parameter dim is being used outside the function. (gcc in C99 mode can
compile the code.)