Hi, I've just started using MS VC++ 2010 express and I have a problem which when using Dev C++ I've never had...

VC++ doesn't allow me to allocate elements for an array using a variable as a subscript, like this :

int maxElements = 10;
char myArray[maxElements];

It says :

error C2057: expected constant expression

Is there a workaround for this? Am I better off using a different compiler such as Dev C++?

tnks
Dean

Any compiler will tell you that's wrong.
When you allocate an array statically(as oppose to dynamically) the size of the array _must_ be available at compile time. In your example, maxElements is a variable so the compiler has no idea if it's going to change at runtime.

So you have 2 options:
- allocate the array dynamically
- make maxElements a 'const int'

If you don't know how, ask ;)

lol, I've just tried it again on Dev C++ and again it compiled and executed as I wanted it to.

Anyway tnks for the suggestions....

lol, I've just tried it again on Dev C++ and again it compiled and executed as I wanted it to.

Anyway tnks for the suggestions....

dev c++ is old and bad compiler it is not standard-compliant you better off using vc++ 2010
follow the the advice the previous poster gave you you will write more efficient and standard compliant code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.