First semester programming student here. For context, we are studying pointers in c++, and are given examples of how to use them in order to initialize arrays with a variable length. For example,
int length;
int *p;
cout<<"Please enter the length of the array: ";
cin>>length;
p = new int[length];
However, for experimentation, I wanted to test the following code to see what errors I get...
int main(){
int length;
cout << "Please enter the length of the array: " << endl;
cin >> length;
int array[length]; //standard array initialized after recieving input from user
for(int i=0; i<length; i++){
array[i] = rand()%100;
}
for(int i=0; i<length; i++){
cout << array[i] << " ";
}
return 0;
}
My confusion comes from the fact that this code executed just fine. I can test with various input lengths and the array will fill and print just fine. Can anyone explain why this might be the case? Thank you for your time.