Why is this not a constant value?

I'm trying to use a global constant variable "SIZE" for use in declaring an array in a function, but even with passing the value to the function, my compiler (Microsoft Visual C++) gives me an error message: "1>c:\users\danny\documents\visual studio 2010\projects\demo\demo\exercise 4-30 bucket sort.cpp(41): error C2057: expected constant expression". Any idea what's going on? A summary of the relevant parts of my program follows...

const int SIZE = 12;

void calledFunction(int [], const int);

int main()
{
int a[SIZE];

calledFunction( a, SIZE );

return 0;
}

void calledFunction( int a[], const int SIZE)
{
int b[10][SIZE]; //this is the line where the error occurs, saying SIZE is not a constant value
}
In the context of calledFunction, SIZE may be const int, but that does not mean that the origin of the number was a constant. That only means that it will not be edited inside that function. What's stopping someone from doing this:

 
calledFunction(a, rand());


Then SIZE would change during runtime, and you cannot initialize a static array with a non-constant size.
Last edited on
A constant isn't enough, a constant expression must be evaluable at compile time.
Hmmm, ok, I understand the error I was making. Thanks for the quick reply!
Topic archived. No new replies allowed.