Array length specifications?

Hi all,
I am new to C++ and I have a doubt.

1
2
  int x = 2;
  int a[x];


For the above code , VC++ 2010 shows the error.

Intellisense: expression must have a constant value.


Whereas compilers like Codeblocks and Dev C++ run this without any error.
Why is this?

Thanks,
-Himansh
This is not standard C++. Some compilers have an optional extension to allow this non-standard code to compile.
1
2
    int x = 2;
    int a[x];


While you are learning the language, it is better to stick to standard C++ which will work on any compiler.
1
2
    const int x = 2;
    int a[x];


What this means is that the size of the array must be known at compile-time (when your source code is being translated into an executable program) rather than at run-time (when you later run the resulting program).
Last edited on
@Chervil:
Can you please recommend me a good compiler which strictly follows STandard C++ Rules
Any worthwhile compiler will have options you can set to disable any extensions and conform strictly to the standards. Have a look at the documentation for your compiler, and I'm sure you'll find it.
Mostly it's a matter of configuring the compiler with the correct options.

Orwell DevC++ can be passed the appropriate compiler options, try this:
-std=c++11 -Wall -Wextra -pedantic-errors

Similar settings can be used with code::blocks by clicking the corresponding check-boxes.
Last edited on
For windows, I recommend minGW.
@Chervil:
Sorry, I am a beginner and I don't know how to do this in Codeblocks or DevC++.
Please give me a little more explanation on how to do this in codeblocks?

Thanks
In Code::blocks, select from the menu:
Settings->Compiler
and check the appropriate boxes corresponding to the settings below
http://i39.tinypic.com/o6wqif.jpg

In DevC++ (Orwell version), from the menu:
Tools->Compiler Options

Check the box marked "Add the following commands when calling the compiler" and enter in the text box:
-std=c++11 -Wall -Wextra -pedantic-errors
Last edited on
Thanks Chervil,

I did as you said in Codeblocks and then I tried to compile the following code.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main(){
    int number = 0;
    cout << "Enter a no. " << endl;
    cin >> number;
    int myArray[number];
    cout << sizeof(myArray) << endl;
    return 0;
}


According to strict C++ Standards, the above code should produce an error, since we cannot set length of array as a variable in Standard C++.

When it asks for a no. I enter 3, Instead of producing an error, it shows the output 12 (because of sizeof). Why is this happening?

(and please enable your private messenger)
Last edited on
When I compile the above code, this is the result:
error: ISO C++ forbids variable length array 'myArray' [-Wvla]

Of course this means the code doesn't run at all as the compiler rejected it, so the rest of the question doesn't make sense.

I can only assume you are using a different compiler or different settings.

I'd like to enable private messages but it only causes disappointment as I never notice them until two weeks later.
Last edited on
Oh, I tried again and it worked. It means that the compiler needs to be restarted before it works.
Thank you for you consistent Support.
I have my Private messenger enabled. Please mail me your email id so that I can contact you whenever I need help on something( since your explanation is very clear ).

Thank you again. Chervil.
Glad it was working. I'll consider your request - and thanks for the vote of confidence.
Topic archived. No new replies allowed.