2

When I use the constant argument of a function as a array size in C++ it gives "Constant expression required" error:
enter image description here

So the compiler is not considering m as a constant object, this means I can change the value of m inside the function, but when I try to increment the value of m it gives "cannot modify a const object" error:
enter image description here

It is really ambiguous to me. Can anyone please explain what I'm getting wrong?

10
  • 14
    HOLY COW what program is this??? It looks like it's from before when I was born.. O_O
    – user541686
    Jan 29, 2012 at 7:23
  • 3
    Whoa, another person with a time machine! Voting to close as "too localized" since no one in the past 10 years has actually used Turbo C++. I wish all programming problems were as simple as "upgrade your compiler, solve all your problems". Jan 29, 2012 at 12:14
  • 1
    Turbo Vision apps... F5 to maximize, F6 next window, Alt-X exit, wordstar keybindings (^K^B block begin etc). I do have a memory for key bindings. I loved me my Borland Turbo Pascal 5.5/6.0 too
    – sehe
    Feb 2, 2012 at 8:24
  • 3
    @Cody: The problem has nothing to do with the age of the compiler, so your close vote is not appropriate here.
    – Xeo
    Feb 2, 2012 at 16:32
  • 1
    @Xeo: Then consider it a close vote because the code is shown in image form. Feb 3, 2012 at 2:22

4 Answers 4

5

const doesn't actually mean "this value is constant", but, "you cannot change this value."

With this, it should be clear why the latter code fails to compile but the former cannot. Even though you give it a default of 5, that doesn't guarantee it's five, and so you don't have a constant, so you can't make an array. But the type is still const, so you can't change it.

That said, since C99 you can have variable-length arrays so this would actually be fine. (It is not okay in C++.) Your compiler just seems to be too old to support C99. (I highly recommend using the latest GCC.)

1
  • 2
    @Imran: in CodeBlocks, are you compiling with the -ansi or -std=c++98 flags? And with warnings on? Feb 2, 2012 at 7:23
4

You cannot create statically a variable-sized array. That is what malloc() is for. The compiler treats the constants as a variable even if they are delclared as const, because it knows nothing about them at compile time, so it can't produce compile-time code to create a static array.

Furthermore: if you declare m as const int m=5, why on Earth do you expect it to be able to increment? It's a constant, so you cannot possibly modify its value.

Nothing is ambigous here, you have just to think a bit about it.

1
  • 1
    I think C99 allows this by using something like alloca underneath?
    – user541686
    Jan 29, 2012 at 7:24
2

CodeBlocks is an editor/IDE, not a compiler. I assume it uses GCC for a compiler, which I think has an extension to use C99's variable-length arrays in C++.

This is not even in C++11 (and probably never will be in C++, since we have std::vector), so this is one of the very few cases where a 20 year old compiler actually got it right.

If I was you, though, I'd still dump TC++. It belongs into a museum.

0

You need a compile-time constant integral expression. Not just a const.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.