Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: is it possible to point to a slice of an array without malloc or VLAs? Date: Wed, 28 Aug 2024 08:33:32 -0700 Organization: A noiseless patient Spider Lines: 30 Message-ID: <86ed68zmpf.fsf@linuxsc.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Wed, 28 Aug 2024 17:33:33 +0200 (CEST) Injection-Info: dont-email.me; posting-host="6b5ad5f5888a1a6f98ce057847c0415f"; logging-data="3703286"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18AlizFEjZv/jjylsypQrilENx2QjU++Jw=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:0k0gDrsjfOYteVtuaubF3sQy6Uw= sha1:PRl41N/FB/fXwbD5i1nDNFz5aQc= Bytes: 1938 Blue-Maned_Hawk writes: > Mark Summerfield wrote: > >> I'm using getopt_long() to process a command line. >> So after a typical call I might have: >> >> argv == {"./efind", "-D", "-x", "one", "two", "three", "four"} >> optind == 3 >> >> What I'd like to do (without copying or mallocing and without >> using a VLA) >> is to get a pointer to a slice of argv, specifically, >> {"one", "two", "three", "four"}. >> In Python terms argv[optind:argc]. >> >> Is this possible in C? > > Yes. > > const char * argv_slice[argc - optind] = &argv[optind]; Presumably the declaration that was intended is something more like const char *(*argv_slice)[argc - optind] = (void*) &argv[optind]; It should be noted that this solution satisfies the stated requirement of not using a VLA, but it does rely on using a variably modified type.