0

I'm trying to do something like this:

const int array_size = 5; string stuff[array_size]; 

My compiler won't let me compile this, even though array_size is a constant. Is there a way to do this without dealing with dynamic arrays?

Edit: "error C2057: expected constant expression"

6
  • 6
    please add a tag for the language you are using Commented Sep 9, 2013 at 21:27
  • 3
    Why doesn't your compiler allow that? Copy and paste the error message. Commented Sep 9, 2013 at 21:28
  • "error C2057: expected constant expression" Commented Sep 9, 2013 at 22:24
  • This code compiles just fine for me as C++ in Visual Studio 2010. Commented Sep 9, 2013 at 22:31
  • 1
    That's fine in (standard) C++, but not in some dialects of C. Are you sure you're compiling C++, with a C++ compiler? Commented Sep 10, 2013 at 0:03

5 Answers 5

5

I have answered this question assuming you are either coding in C or C++. If you are using a different language, this answer doesn't apply. However, you should update your question with the language you are trying to use.

Consider the following program:

int main () { const int size = 5; int x[size]; return 0; } 

This will compile in both C++ and C.99, but not C.89. In C.99, variable length arrays were introduced, and so locally scoped arrays can take on a size specified by a variable. However, arrays at file scope in C.99 cannot take a variable size parameter, and in C.89, all array definitions have to have a non variable size.

If you are using C.89, or defining a file scope array in C.99, you can use an enum to name your constant value. The enum can then be used to size the array definition. This is not necessary for C++ however, which allows a const integer type initialized by a literal to be used to size an array declaration.

enum { size = 5 }; int x[size]; int main () { return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

0
#define array_size 5 string stuff[array_size]; 

5 Comments

Why do you think this will fix the problem?
@minitech Why do you think it won't? #defines cause textual substitution so the result should be identical to string_stuff[5];.
@arshajii: I was more prompting an explanation instead of a block of code. Why wouldn’t the original code work? We don’t even know the error. Or the language until two minutes ago, for that matter — heck, it could have been C#. An answer should explain these things.
@minitech Okay, I guess I misinterpreted your comment. By the way, how do we know the language?
@arshajii: The tag on the question. It was just added.
0

You can use e.g. a vector or the new keyword to allocate memory dynamically, because declared arrays can not have runtime sizes.

Comments

0

Only thing I can think of is that you defined another array_size variable in your code, which is not a compile time constant and hides the original array_size.

Comments

-1

array_size is not treated as a compile time constant. Constness added just makes sure that programmer can not modify it. If tried to modify accidentally, compiler will bring to your attention.

Size of an array needs to be a compile constant. Seems like your compiler is not supporting Variable Length Array. You can #define the size of the array instead which is treated as a constant expression.

3 Comments

But it is a compile time constant in C++. The code is legal c++.
I am pretty sure that I read in some SO thread,long time back, that constness added is not treated as a compile time constant.
Ok. Let me find it. Will delete my answer later. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.