Here is a simple code of defining an array. I noticed this code will work(compile and run) under Linux(OpenSue,gcc compiler), but it will not work under Windows system. The compiler gave error prompt. Does anybody know the reason? Thanks!
#include <iostream>
using namespace std;
int main()
{
int N;
cin>>N;
int ar[N];
ar[0]=0;
cout<<"ar[0]= "<<ar[0]<<endl;
return 0;
}
The code isn’t valid C++ since C++ does not allow declaring a (stack-allocated) array with a variable size as you do. The reason for this is that C++ offers better mechanisms of declaring dynamically sized arrays, using the
std::vectorclass from its standard library:g++(the compiler you used on Linux) by default allows this through a compiler extension.