Deutsch English Français Italiano |
<20250209123918.0000754f@yahoo.com> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Michael S <already5chosen@yahoo.com> Newsgroups: comp.lang.c Subject: Re: Two questions on arrays with size defined by variables Date: Sun, 9 Feb 2025 12:39:18 +0200 Organization: A noiseless patient Spider Lines: 105 Message-ID: <20250209123918.0000754f@yahoo.com> References: <vo9mns$gsd7$1@dont-email.me> <vo9nn3$gtph$1@dont-email.me> <vo9u0u$i0n8$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Sun, 09 Feb 2025 11:39:19 +0100 (CET) Injection-Info: dont-email.me; posting-host="8e2d9f7cf4b47825ab2c863bbd44b8cd"; logging-data="566868"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ACOdRt15QcCJB5PfSgT3Y/bvn4N6JttA=" Cancel-Lock: sha1:m5aqjQ4f2mbF6fnNTjUnOlqWSuA= X-Newsreader: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32) Bytes: 4186 On Sun, 9 Feb 2025 10:54:36 +0100 Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: > On 09.02.2025 09:06, Andrey Tarasevich wrote: > > 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. > > Oh, sorry, no; above I had just written an excerpt. - Actually I had > those two examples above within a main() function. - Sorry again for > my inaccuracy. > > What I meant was (with surrounding context) that I knew (from _other_ > languages) a syntax like > > main () > { > int n = 5; > > { > char * arr[n]; > ... > } > } > > And in "C" (C99) I tried it *without* the _inner block_ > > main () > { > int n = 5; > char * arr[n]; > ... > } > > and it seemed to work that way. (In those other languages that wasn't > possible.) > > > > >> 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. > > I fear I don't understand what you're saying here. - By "now" do you > mean newer versions of the C standards? That you can rely only, say, > rely on it with C99 but maybe not before and not in later C standards > conforming compilers? > Yes, theoretically. In practice, I am not sure that there exists fully conforming C17 or especially C23 compiler that does not support VLA. But there exists one important almost-C17 compiler that does not support VLA. There is another problem in your code - it assigns string literal to non-const char*. It is legal, as far as 'C' Standard is concerned, but makes very little practical sense, because any attempt to assign to string literal through resulting pointer is UB. And not just a theoretical UB, but a real-world UB. > For my purpose it would be okay to know whether with the C99 version > (that I used) it's okay, or whether that's some GNU specific extension > or some such. > > Janis > > > > >> [...] > >