Sujet : Re: is it possible to point to a slice of an array without malloc or VLAs?
De : mark (at) *nospam* qtrac.eu (Mark Summerfield)
Groupes : comp.lang.cDate : 29. Aug 2024, 08:52:02
Autres entêtes
Message-ID : <4Z6dnTcuH5c_tU37nZ2dnZfqn_qlxKTP@brightview.co.uk>
References : 1 2
User-Agent : Pan/0.154 (Izium; 517acf4)
On Wed, 28 Aug 2024 09:13:39 +0100, Phil Ashby wrote:
[snip]
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.
[snip]
Thank you, that works great. I now do that plus store the slice's size using:
argc - optind
I tried to get the size using
#define ARRAYSIZE(a) (&(a)[1] - (a))
char **folders = argv + optind;
int num_folders = argc - optind;
int size = ARRAYSIZE(folders);
printf("%d %d\n", num_folders, size);
but size was always 1.