-
variable for array ?
hi all~
i remembered one-dimentional array requires const as argument when it was established but now i can pass variable to it ? i update Dev-C++ not long ago i wonder whethere it was exception of Dev-C++ compiler, my code is right below:
Code:
int main()
{
int n = 10;
int arr[n]; // No error occured !!!
return 0;
}
any ideas ?
-
Some compilers have an extension that allows non-constant array sizes. You can use it, but your code won't be portable. The standard way to do this is with dynamic memory allocation:
Code:
int
main()
{
int n = 10;
int *arr = new int[10];
...
}
But of course, the standard library should be preferred over ad hoc code:
Code:
#include <vector>
int
main()
{
int n = 10;
std::vector<int> arr(n);
...
}
-
That's valid in C99 and is enabled by default as a compiler extension in GCC.
Ref: http://gcc.gnu.org/onlinedocs/gcc-3....iable%20Length
gg