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?
At the moment I store argv, optind, and argc and handle the sliceI don't think so, other than to drop the outer check as the loop
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.
argv == {"./efind", "-D", "-x", "one", "two", "three", "four"}. . .
In Python terms argv[optind:argc].
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?
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];
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];
To answer the specific question, I would use pointer arithmetic,[snip]
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.
On Wed, 28 Aug 2024 09:13:39 +0100, Phil Ashby wrote:
[snip]
To answer the specific question, I would use pointer arithmetic,[snip]
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.
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.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 148:49:58 |
Calls: | 10,383 |
Calls today: | 8 |
Files: | 14,054 |
D/L today: |
2 files (1,861K bytes) |
Messages: | 6,417,760 |