I can define the length of an array using a constant, so why doesn't int d[b] work?

what are the differences between b and c?

c has a compile time constant initialiser, while b does not. A const object with compile time constant initialiser is itself a compile time constant value.

Since I can define an lenth of an arry using a constant ,so why don't this work?

Not just any constant will do. const qualifier implies runtime constness (i.e the value may be determined at runtime but won't change throughout the lifetime of the object). Only compile time constant values can be used as array size.


You are using a non-constant variable to assign value to a constant. Therefore, that variable's value can't be determined compile time. I know you aren't changing a, but the compiler does not think like this.


The compiler diagnostic should really be compile time evaluable constant expression.

Since the original object to which b is assigned is not const, b is not a compile time evaluable constant expression, so compilation will fail as variable length arrays are not supported in standard C++.

Tags:

C++