Visual Studio is giving error while compiling this c++ code. It says that size should be a constant variable. I have tried making it constant but it does not work.
int size; cout << "Please Enter the array size : " ; cin >> size; int myArr[size]; The size of an array must be known at compile time. In your case, the size is known at runtime so you must allocate the array from the heap.
int size; std::cin >> size; int* myArr = new int[size]; // ... delete[] myArr; std::vector (e.g. because there is no simple way to resize it without initializing the new elements, or because it's slightly larger than a plain pointer), then use std::unique_ptr<T[]>.auto array = std::make_unique<int[]>(n);. Under the hood it's no different from manually using new/delete, except you can't forget to delete the memory. Also you get exception safety: memory is freed even if something throws. On the other hand, with manual new/delete calls there is no sane way to work with exceptions without getting leaks.
alloca/_alloca, which have the same effect.