Having variable for array index in C/C++

Hi there, can explain if this code is allowed?

int n;
int arr[n];

Usually need a const for array index size. Anyhow it working for some code without errors.

If you need to allocate an array with a dynamic size you should do one of the following:

C:
int* arr = (int*)malloc(n*sizeof(int));

free(arr);

or

C++:
int* arr = new int[n];

delete [] arr;

Note that if you are using C++ I would recommend using std::vector instead of native arrays.