Reputation: 5924
I am new to C++.I was going through a C++ book and it says
const int i[] = { 1, 2, 3, 4 };
float f[i[3]]; // Illegal
It says the declaration of the float variable is invalid during compilation.Why is that?
Suppose if we use
int i = 3;
float f[i];
It works.
What is the problem with the first situation?
Thanks.
Upvotes: 9
Views: 423
Reputation: 71899
So the first is illegal because an array must have a compile-time known bound, and i[3]
, while strictly speaking known at compile time, does not fulfill the criteria the language sets for "compile-time known".
The second is also illegal for the same reason.
Both cases, however, will generally be accepted by GCC because it supports C99-style runtime-sized arrays as an extension in C++. Pass the -pedantic
flag to GCC to make it complain.
Edit: The C++ standard term is "integral constant expression", and things qualifying as such are described in detail in section 5.19 of the standard. The exact rules are non-trivial and C++11 has a much wider range of things that qualify due to constexpr
, but in C++98, the list of legal things is, roughly:
const
and initialized with a constant expressionUpvotes: 15
Reputation: 8120
For arrays with lengths known only at runtime in C++ we have std::vector<T>
. For builtin arrays the size must be known at compile-time. This is also true for C++11, although the much older C99-standard already supports dynamic stack arrays. See also the accepted answer of Why doesn't C++ support dynamic arrays on the stack?
Upvotes: 0
Reputation: 15872
Just to expound on Sebastian's answer:
When you create a static array, the compiler must know how much space it needs to reserve. That means the array size must be known at compile-time. In other words, it must be a literal or a constant:
const int SIZE = 3;
int arr[SIZE]; // ok
int arr[3]; // also ok
int size = 3;
int arr[size]; // Not OK
Since the value of size
could be different by the time the array is created, the oompiler won't know how much space to reserve for the array. If you declare it as const
, it knows the value will not change, and can reserve the proper amount of space.
If you need an array of a variable size, you will need to create it dynamically using new
(and make sure to clean it up with delete
when you are done with it).
Upvotes: 2
Reputation: 4327
Your second example doesn't work and it shouldn't work.
i
must be constant. This works
const int i = 3;
float f[i];
Upvotes: 3