Sujet : Re: is it possible to point to a slice of an array without malloc or VLAs?
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.cDate : 28. Aug 2024, 10:50:54
Autres entêtes
Organisation : Stefan Ram
Message-ID : <slice-20240828104917@ram.dialup.fu-berlin.de>
References : 1
Mark Summerfield <
mark@qtrac.eu> wrote or quoted:
argv == {"./efind", "-D", "-x", "one", "two", "three", "four"}
. . .
In Python terms argv[optind:argc].
#include <stdio.h>
int main()
{ char const * const argv[] =
{ "./efind", "-D", "-x", "one", "two", "three", "four" };
size_t const argc = sizeof( argv )/ sizeof( 0[ argv ]);
printf( "%zu\n", argc );
char const * const * rest = argv + 3; /* <<< answer to the question */
for( char const * const * entry = rest; entry < argv + argc; entry++ )
printf( "%.32s\n", *entry ); }
If that's not your jam, you can pretty much 86 all the "const"
declarations in the program above. But if you're rolling
in from Python, where "const" is ghost, it's hella tight
to be able to use "const" again. You'd be missing out on the
gnarliest feature in C!
In Python, slices are always copied. Here in the C program
above, the "slice" shares memory with the base array.
(There's no such thing as "slices" in C.)