Ahem A Rivet's Shot writes:
[...]
> For a more dramatic example of efficiency consider strtok(), the
> routine at the heart of turning a string into argc and argv. It works by
> injecting nulls into the string where the spaces are and returning an array
> of pointers to the starts of the words in the original string - In other
> words it turns a single string into several strings and returns them in an
> array - and it does so in place without using more memory than is
> absolutely essential. Doing string manipulation in place with minimal
> overhead was very important at the time C was designed.
Quibble: No, strtok() doesn't return an array of pointers (for the
simple reason that a C function cannot return an array value). It
returns a single string pointer on each call. You have to call it
multiple times to get pointers to all the tokens. It uses its own
static memory to keep track.
Incidentally, I don't believe strtok is used to turn a string into argc
and argv. That's handled by the shell, and tokenizing on white space
doesn't work. For example:
echo arg1 'arg 2'
passes two arguments to echo, not three. (bash does use strtok in some
places; I'm not sure just how it's used.)
[followups to comp.lang.c]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)
|