0

Possible Duplicate:
In C++ books, array bound must be constant expression, but why the following code works?

I do this within main():

int i = 2; //not constant expression
int foo[i]; //no error?

This doesn't give me an error as it should according to my book. Why?

(i'm on clang 3.1)

Community
  • 1
  • 1
user2015453
  • 4,844
  • 5
  • 25
  • 27

2 Answers2

6

Compile it with strict warnings enabled.
-pedantic if you use gcc and it will give you an error.

Most compilers support variable length arrays(VLA) through a language extension. However the C++ language standard does not support VLA as a part of the language.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I'm on clang 3.1 - updated question. -pedantic gives me the warning "variable length arrays are c99 feature" though. – user2015453 Jan 27 '13 at 14:32
  • 1
    @user2015453: So it gives you a warning. A diagnostic is all that a compiler needs to provide you for using a non standard language extension. It does. – Alok Save Jan 27 '13 at 14:34
  • @user2015453 Try to add `-Werror`; I know this compiler flag from GCC but maybe Clang supports it, too. It makes compilation fail not only on errors but also on warnings (or turns warnings into errors, if you want to express it that way) – leemes Jan 27 '13 at 14:35
0

Variable-length automatic arrays are allowed in ISO C99... for more detail have a look at http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Variable-Length.html#Variable-Length

Saqlain
  • 17,490
  • 4
  • 27
  • 33