Sujet : Re: is it possible to point to a slice of an array without malloc or VLAs?
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.cDate : 28. Aug 2024, 16:29:58
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87bk1cy8ax.fsf@bsb.me.uk>
References : 1 2
User-Agent : Gnus/5.13 (Gnus v5.13)
Blue-Maned_Hawk <
bluemanedhawk@invalid.invalid> 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];
First, the OP did not want to use a VLA and, second, that initialisation
is invalid. Third, if a VLA is used, the pointers would be copied and
the OP did not want that either.
-- Ben.