+ 1
What does Array bound is not an integer constant before ']' token mean??
On internet I have read that array size should be known at the compilation time so that the memory needed for it is known at compile time. But my doubt is in the following code : #include <iostream> using namespace std; Int main(){ int n; cin>>n; int a[n]; for(int i=0; i<n; i++){ cin>> a[i]; cout<<a[i] <<endl; } return 0; } In this code, at the compile time the value of n is not known which is the size of the array but still this code runs... But why?? Can anyone please explain.
1 Réponse
0
You are using <n> to specify array dimension, but <n> here is an `int` variable (not a constant). That is the explanation for the error message.
Defining an array that way means you are using VLA (Variable Length Array), which is not a part of the standard, so you may expect different outcome with different compiler.
* About VLA
https://en.m.wikipedia.org/wiki/Variable-length_array
* VLA is not part of standard
https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard
You can use `int` variable to specify array dimension by using dynamic memory allocation, as SoloLearn had taught us in the C++ tutorial - the following chapter 👇
https://www.sololearn.com/learn/CPlusPlus/1632/