Reputation: 11
I was just going through some simple C++ concepts. I like to think that I am aware of the difference between dynamic and static arrays. But when I run the following code:
`
#include <iostream>
using namespace std;
int main()
{
int size;
cout<<"enter size: ";
cin>>size;
int arr[size];
cout<<"enter array values: ";
for(int i=0;i<size;i++)
cin>>arr[i];
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
return 0;
}
`
It does not give me an error. It shouldn't let me create a static array with the size input from the user right?
Upvotes: 1
Views: 144
Reputation: 34634
You cannot. This is not valid C++.
It compiles because the compiler you use offers extensions beyond by the C++ standard. Your compiler would have warned you about it if you had enabled compiler warnings:
-Wall -Wextra -pedantic
Upvotes: 5