2

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

see this code

#include<iostream>
int main
{
using namespace std;
int a=7;
char arr[a];
return 0;
}

in my book it is written that array_size must be a constant but codeblocks is not giving error in it...

it is not the only case.. there are several other examples also.

who is wrong IDE, compiler, or any other thing???

does using newer or older book can cause such issue??

Community
  • 1
  • 1
chandola
  • 126
  • 1
  • 10
  • I am pretty sure that you can't do that unless a is const. – mathematician1975 Jun 21 '12 at 18:35
  • 2
    @KenWhite: No, `a` is not a constant expression. A compiler *can* determine that the value is `7`, but it's not required to. The language standard's definition of a *constant expression* is designed not to require compilers to perform dataflow analysis. – Keith Thompson Jun 21 '12 at 18:40
  • That is sneaky though - clearly it works but is actually right?? Right in the sense of valid C++ I mean. – mathematician1975 Jun 21 '12 at 18:40
  • @KenWhite: The question asked was weather or not it is legal to declare the array in this way. That is a question about the language, and therefore a Standards question. – John Dibling Jun 21 '12 at 18:52
  • @JohnDibling, at the risk of repeating myself: I **did not post an answer**; I didn't address the legality within the standard. I merely explained **why this particular code was compiling**. Sheesh! – Ken White Jun 21 '12 at 18:54
  • @KenWhite: OK, no sweat. I was simply responding to your comment that "the question wasn't about the standard." – John Dibling Jun 21 '12 at 18:56
  • @JohnDibling: No offense taken - thought I'd added a smile after the "Sheesh!". :-) I was pointing out to both you and Keith that I wasn't trying to answer the question asked - had a minor error in the phrasing of my first reply to Keith is all. :-) – Ken White Jun 21 '12 at 20:42

4 Answers4

8

The code is ill-formed. The extent of an array must be a constant expression, and i is not a constant expression (it is not declared const).

If a were declared as a const int and initialized with a constant expression (like 7), the code would be well-formed because the extent would then be a constant expression. For example, the following is well-formed:

int main()
{
    int const a = 7;
    char arr[a];
}

Some C++ compilers (including gcc, in some compilation modes) support variable length arrays, which are a feature from C, and which allow local arrays to have nonconstant size. Compilers that provide this feature do so as a language extension, though; it is not a feature of C++.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
1

Since C99 (ISO/IEC 9899:1999, section 6.7.5.2), this is legal in C: arr is a variable length array (VLA). However, there is no VLA in C++. The accepted answer here explains why.

Community
  • 1
  • 1
kol
  • 27,881
  • 12
  • 83
  • 120
0

It is illegal in C++98, C++03, C++11, C89 and C90 (probably in C11 also).

It is legal only in C99.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

It is not legal according to the Standard, here:

C++03 8.3.4/1 [dcl.array]

In a declarationT D where D has the form D1 [constant-expressionopt] and the type of the identifier in the declarationT D1 is “derived-declarator-type-list T,” then the type of the identifier of D is an array type. T is called the array element type; this type shall not be a reference type, the (possibly cv-qualified) type void, a function type or an abstract class type. If the constant-expression (5.19) is present, it shall be an integral constant expression and its value shall be greater than zero. [...]

Integral Constant Expressions are further defined in 5.19/1:

[...] An integral constant-expression can involve only literals (2.13), enumerators, const variables or static data members of integral or enumeration types initialized with constant expressions (8.5), non-type template parameters of integral or enumeration types, and sizeof expressions.

John Dibling
  • 99,718
  • 31
  • 186
  • 324