[Bug c++/51352] New: g++ can't pass variable-length array as a function argument
jvgreen at gmail dot com
gcc-bugzilla@gcc.gnu.org
Wed Nov 30 08:38:00 GMT 2011
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: unassigned@gcc.gnu.org
ReportedBy: jvgreen@gmail.com
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.)
More information about the Gcc-bugs
mailing list