Sujet : Re: Two questions on arrays with size defined by variables
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 09. Feb 2025, 21:16:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vob2fo$q3rn$1@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 2/9/25 03:06, Andrey Tarasevich wrote:
On Sat 2/8/2025 11:50 PM, Janis Papanagnou wrote:
...
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.
While that is correct, it's incomplete. The relevant constraint is
"If an identifier is declared to be an object with static or thread
storage duration, it shall not have a variable length array type."
(6.7.6.2p2).
All objects declared at file scope have static storage duration, but
that rule also applies to objects objects declared at block scope if
they are declared with the "static" or "thread_local" keywords.