0

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

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by alaukik
1

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 ;)

Edited by thelamb: n/a

0

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....

1

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 :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.