Deutsch   English   Français   Italiano  
<vamm7j$3d74u$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Phil Ashby <phil.eternal@ashbysoft.com>
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 09:13:39 +0100
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <vamm7j$3d74u$1@dont-email.me>
References: <NS2dnaQtUKseUVP7nZ2dnZfqn_WdnZ2d@brightview.co.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 28 Aug 2024 10:13:39 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5e8acb0a16d8c263a190770039c93041";
	logging-data="3579038"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/koyBnCNbO1zeZWzf/OJGG"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ROdmrSOCLr9EU1lNlwxyFVX18t4=
In-Reply-To: <NS2dnaQtUKseUVP7nZ2dnZfqn_WdnZ2d@brightview.co.uk>
Content-Language: en-US
Bytes: 2259

On 28/08/2024 08:06, 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?

To answer the specific question, I would use pointer arithmetic,
provided there is no intention to modify values, ie:

	char **slice = argv + config->optind;

thus slice now points at the appropriate part of argv and can be indexed
or dereferenced / incremented to access elements.

> At the moment I store argv, optind, and argc and handle the slice
 > using a loop:
> 
> if (config->optind < config->argc) for (int i = config->optind; i <
> config.argc; ++i) process(config->argv[i]);
> 
> This works fine, so really I'm just wondering if there's a nicer
> way.
I don't think so, other than to drop the outer check as the loop
condition provides the same boundary.

Phil.