[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: One more C question




Jeff Licquia wrote:
> 
> On Tue, May 02, 2000 at 12:05:45AM -0400, Jordan Bettis wrote:
> >
> > Is there any way to find the size of an array that is refrenced by a
> > pointer? I need to find the size of **argv (or *argv[] if you would
> > prefer). wouldn't sizeof() just give me that of the pointer, not what it
> > is pointing to? I need to find the size of one string, not the entire
> > array of strings. so it would be like argv[known][need to know].
> 
> There's no way of finding the number of elements in an array "by
> default".  For null-terminated character arrays (otherwise known as
> strings), you can use strlen().  In the special case of argv, the
> number of arguments is in argc, and (I believe) argv[argc] = NULL.
> (That may be specific to some compiler I used, so don't quote me on
> it.)
> 
> So, you could do this to get the length of the last argument:
> 
>   len = strlen(argv[argc-1]);

Aren't arrays kept contiguous in memory?  So do some math on the
pointers, subtracting the last character in argv[argc-1] from argv[0]. 
If not, then you'd have to do a for/while loop and add up the length of
each string.

--
To unsubscribe, send email to majordomo@luci.org with
"unsubscribe luci-discuss" in the body.