Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #387917

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

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: is it possible to point to a slice of an array without malloc or VLAs?
Date 2024-08-28 08:33 -0700
Organization A noiseless patient Spider
Message-ID <86ed68zmpf.fsf@linuxsc.com> (permalink)
References <NS2dnaQtUKseUVP7nZ2dnZfqn_WdnZ2d@brightview.co.uk> <pan$97210$7f2c1504$c93772a0$7dfab761@invalid.invalid>

Show all headers | View raw


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.

Back to comp.lang.c | Previous | NextPrevious in thread | Find similar


Thread

is it possible to point to a slice of an array without malloc or VLAs? Mark Summerfield <mark@qtrac.eu> - 2024-08-28 07:06 +0000
  Re: is it possible to point to a slice of an array without malloc or VLAs? Phil Ashby <phil.eternal@ashbysoft.com> - 2024-08-28 09:13 +0100
    Re: is it possible to point to a slice of an array without malloc or VLAs? Mark Summerfield <mark@qtrac.eu> - 2024-08-29 07:52 +0000
      Re: is it possible to point to a slice of an array without malloc or VLAs? Ben Bacarisse <ben@bsb.me.uk> - 2024-08-29 11:04 +0100
  Re: is it possible to point to a slice of an array without malloc or VLAs? Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-08-28 14:25 +0000
    Re: is it possible to point to a slice of an array without malloc or VLAs? Ben Bacarisse <ben@bsb.me.uk> - 2024-08-28 16:29 +0100
    Re: is it possible to point to a slice of an array without malloc or VLAs? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-28 08:33 -0700

csiph-web