<div dir="ltr">File: <a href="http://gcc-4.2.info">gcc-4.2.info</a>,  Node: Variable Length,  Next: Empty Structures,  Prev: Zero Length,  Up: C Extensions<br><br>5.14 Arrays of Variable Length<br>==============================<br>
<br>Variable-length automatic arrays are allowed in ISO C99, and as an<br>extension GCC accepts them in C89 mode and in C++.  (However, GCC&#39;s<br>implementation of variable-length arrays does not yet conform in detail<br>
to the ISO C99 standard.)  These arrays are declared like any other<br>automatic arrays, but with a length that is not a constant expression.<br>The storage is allocated at the point of declaration and deallocated<br>when the brace-level is exited.  For example:<br>
<br>     FILE *<br>     concat_fopen (char *s1, char *s2, char *mode)<br>     {<br>       char str[strlen (s1) + strlen (s2) + 1];<br>       strcpy (str, s1);<br>       strcat (str, s2);<br>       return fopen (str, mode);<br>
     }<br><br> Jumping or breaking out of the scope of the array name deallocates the<br>storage.  Jumping into the scope is not allowed; you get an error<br>message for it.<br><br> You can use the function `alloca&#39; to get an effect much like<br>
variable-length arrays.  The function `alloca&#39; is available in many<br>other C implementations (but not in all).  On the other hand,<br>variable-length arrays are more elegant.<br><br> There are other differences between these two methods.  Space allocated<br>
with `alloca&#39; exists until the containing _function_ returns.  The<br>space for a variable-length array is deallocated as soon as the array<br>name&#39;s scope ends.  (If you use both variable-length arrays and<br>`alloca&#39; in the same function, deallocation of a variable-length array<br>
will also deallocate anything more recently allocated with `alloca&#39;.)<br><br> You can also use variable-length arrays as arguments to functions:<br><br>     struct entry<br>     tester (int len, char data[len][len])<br>
     {<br>       /* ... */<br>     }<br><br> The length of an array is computed once when the storage is allocated<br>and is remembered for the scope of the array in case you access it with<br>`sizeof&#39;.<br><br> If you want to pass the array first and the length afterward, you can<br>
use a forward declaration in the parameter list--another GNU extension.<br><br>     struct entry<br>     tester (int len; char data[len][len], int len)<br>     {<br>       /* ... */<br>     }<br><br> The `int len&#39; before the semicolon is a &quot;parameter forward<br>
declaration&quot;, and it serves the purpose of making the name `len&#39; known<br>when the declaration of `data&#39; is parsed.<br><br> You can write any number of such parameter forward declarations in the<br>parameter list.  They can be separated by commas or semicolons, but the<br>
last one must end with a semicolon, which is followed by the &quot;real&quot;<br>parameter declarations.  Each forward declaration must match a &quot;real&quot;<br>declaration in parameter name and data type.  ISO C99 does not support<br>
parameter forward declarations.<br><br></div>