-1

This is giving me an error that expression a must have a constant value. I cannot understand what is wrong with this method. n is an integer and I am giving it the array size. Can somebody help me to understand this problem?

int fib(int n)
{
const int a = n;
int f[a];
...
}
ThunderStruct
  • 1,504
  • 6
  • 23
  • 32
ram singh
  • 41
  • 5

1 Answers1

1

Others have said this already, but essentially the problem is that you are assigning the array a size that is variable and can only be known at runtime of the program. The compiler has to know what size the array is going to be at compile time, so that's a problem and conflicts with the C++ standard. Notably some compilers do have extensions that support this, so that could be your confusion. I would recommend using std::vector instead for anytime you need variable length arrays.

samuelnj
  • 1,627
  • 1
  • 10
  • 19