Reputation:
Dumb question. but can you do this. (these are global variables by the way)
int size;
double Values[size];
If Im getting the SIZE from a file?
I know you probably can't, but maybe theirs some way to readjust the size based on the number I read in from the file (like say I read in 7, I know Values will have to be of Size 7).
The compiler complains, but Im wondering if their is some workaround. (these have to stay as global variables btw.
Upvotes: 2
Views: 1727
Reputation: 72261
You can, actually :)
C99 allows that. MSVC doesn't support C99, GCC does.
C++ compilers also tend to allow that as an extension (answer yourself if you're OK with depending on compiler extensions). The only requirement is that you need to know size
at the moment of declaration of Values
.
A std::vector
is usually better (and safer and standarised and so on) anyway, unless you really really want that data on the stack.
Upvotes: 3
Reputation: 101456
No, you can only specify array sizes like this using constant integral expressions. Meaning known at compile-time.
You probably shouldn't be doing this, anyway. Instead, you should be using a vector.
If for whatever reason you can't use a vector (which I would seriously doubt), then your next option would be to use dynamically-allocated arrays. But please, for the love of all that is good int he world, use a smart pointer if you do this.
Upvotes: 7
Reputation: 57248
You can do it if you allocate them dynamically:
double *Values = new double[size];
...
delete [] Values;
but not statically.
Upvotes: 2