John Bode
5/16/2011 10:03:00 PM
On May 16, 12:52 pm, Alain Spineux <alain.spin...@gmail.com> wrote:
> in
>
> int f(int n)
> {
> int tab[n];
> tab[n]=1;
> return tab[n];
>
> }
>
> What does
> int tab[n];
> means ?
>
> Thanks
C99 introduced the notion of a variable-length array (VLA), where the
array size isn't known until runtime. Note that "variable" simply
means that the array length isn't fixed at compile time. You cannot
change the length of the array after it has been declared.
So, if you called f with the value 10, it would create a 10-element
array of int (indexed 0 through 9). If you called f with the value
1000, it would create a 1000-element array.
VLAs are useful, but you need to be careful with them, and they're not
100% interchangeable with regular arrays (they can't be declared
static or extern, nor can they be members of a struct or union type).
They may be allocated in such a way that if n is too large, you will
exceed the memory available for such variables (whether it's on the
stack or some other mechanism).