Liste des Groupes | Revenir à cl c |
On Sat 2/8/2025 11:50 PM, Janis Papanagnou wrote:
>I've found examples on the Net where the arrays have been defined>
in a function context and the size passed as parameter
>
f(int n) {
char * arr[n];
...
}
Yes, that would be a VLA.
>That reminded me on other languages where you'd need at least a>
block context for dynamically sized arrays, like
>
int n = 5;
{
char * arr[n];
...
}
But a function body is in itself a block. Inside a function body
you are already in "a block context".
>Anyway. I tried it without function or block context>
>
int n = 5;
char * arr[n];
...
>
and it seemed to work seamlessly like that (with GNU cc,
-std=C99).
You mean you did this at file scope? No, VLAs are illegal at file
scope. And I was unable to repeat this feat in GCC.
>Q1: Is this a correct (portable) form?>
VLA objects have to be declared locally. However, keep in mind
that support for local declarations of VLA _objects_ is now
optional (i.e. not portable). Support for variably-modified
_types_ themselves (VLA types) is mandatory. But you are not
guaranteed to be able to declare an actual VLA variable.
Les messages affichés proviennent d'usenet.