Only constants can be used to declare the size of automatic and static
arrays. Not using a constant for this purpose is a compilation error.
Says Pearson.
However, following works fine. According to Pearson, it should not work (I think). Can you help me to understand this?
#include <iostream>
using namespace std;
int main()
{
int in;
cin >> in;
int a[in];
a[4] = 412;
int i = 0;
while(i<5){
cout << i << ":" << a[i] << endl;
i++;
}
}
That’s an extension to C++ that’s found in the GNU compiler, but it doesn’t conform to the C++ standard. If you compile your code using
g++ -std=c++98, the compiler should complain.