Why can't we specify a variable size when declaring a static array?

34 views Asked by At

Through dynamic memory allocation, the following the code works perfectly.

int *ptr;
int size1;

cin >> size1;

ptr = new int[size1];

In static memory allocation, I get the following error: array bound is not an integer constant before ']' token

int size2;
cin >> size2;
int arr[size2];

Why is this so? Why can't we specify a variable size?

1

There are 1 answers

0
Jarod42 On

Variable length array is an extension supported by certain compilers and it not (yet) in the standard.