Re: is it possible to point to a slice of an array without malloc or VLAs?

Liste des GroupesRevenir à l c 
Sujet : Re: is it possible to point to a slice of an array without malloc or VLAs?
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.lang.c
Date : 28. Aug 2024, 17:33:32
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86ed68zmpf.fsf@linuxsc.com>
References : 1 2
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
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];

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.

Date Sujet#  Auteur
28 Aug 24 * is it possible to point to a slice of an array without malloc or VLAs?8Mark Summerfield
28 Aug 24 +* Re: is it possible to point to a slice of an array without malloc or VLAs?3Phil Ashby
29 Aug 24 i`* Re: is it possible to point to a slice of an array without malloc or VLAs?2Mark Summerfield
29 Aug 24 i `- Re: is it possible to point to a slice of an array without malloc or VLAs?1Ben Bacarisse
28 Aug 24 +- Re: is it possible to point to a slice of an array without malloc or VLAs?1Stefan Ram
28 Aug 24 `* Re: is it possible to point to a slice of an array without malloc or VLAs?3Blue-Maned_Hawk
28 Aug 24  +- Re: is it possible to point to a slice of an array without malloc or VLAs?1Ben Bacarisse
28 Aug 24  `- Re: is it possible to point to a slice of an array without malloc or VLAs?1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal