Error while declaring static array inside a function with function parameter
int fun(int x)
{
int a[x]; //No Error
static int b[x]; //Error "storage size of x is not constant
int *f = new int[x+1]; //NO ERROR--NEW OPERATOR USED TO DEFINE THE ARRAY
}
What should be changed in 2nd line inorder to declare the array "b" without any error.
std::vector<>in such casesconst int b[x]is allocating memory at compile time soxcannot be a variable (it's value is not known at compile time), whereasint *f = new int[x+1]allocates memory at run time (because of thenew), soxcan be a variable. As @quantdev says, use a different type, or use dynamic memory allocation, such asint *b = new int[x];.int a[x];is not valid C++ either. It's a C feature that certain compilers, most notably GCC, accept in C++ as a language extension. Do usestd::vectorinstead.