I understand in VS all variables must be declared at the top of a block, but if I want a VLA, ie. if I wanted to do something like this:
int result = runalgorithm();
int vla[result];
the code above is invalid, because vla
must be declared at the top. What is a good solution for this, other than creating an arbitrarily large array?
You cannot. VLA is supported in
C99
and later standards. (Support is mandatory in C99; it is optional in C11.)C89
does not have the VLA concept or support for it.You can choose dynamic memory allocation, instead. have a look at
malloc()
and family for your reference.Remember, if you want to use dynamic memory (allocation), you have to
free()
the allocated memory once you're done using it.