Re: strtok ( ) help



Default User wrote:
> pemo wrote:
>
>> Default User wrote:
>
>>> That's not likely. What it will "remember" is the last pointer value
>>> that it returned, which is an offset into the string (probably just
>>> a static char*. If it made a copy of the string, not only would
>>> that be inefficient, but if an operation changed the original
>>> string between calls to strtok() its copy would no longer match.
>>
>> Yes, you're probably right, thanks for the correction ... hold on,
>> brb <time passes> ... ...
>> yup, certainly looks like it is as you say - for the gcc version at
>> least. Still, makes one wonder whether your comment ["but if an
>> operation changed the original string between calls to strtok() its
>> copy would no longer match"] might either be useful, or else goes
>> against how the docs say strtok() works.
>
>
> Remember also that strtok() has to modify the original string, so it
> has to have a pointer into that string in all cases. That is, it
> doesn't help to work on copy of the string because it has to punch
> null characters in place of the delimiters. ...

> The strtok() syntax and semantics are well into the "cheap, fast, and
> dirty" style.

Yup, ok, and I guess, from 7.21.5.8.2 "A sequence of calls to the strtok
function breaks *the string pointed to by s1* into a ..." [pretty much]
implies that a copy should *not* be made [hmmmm ????]. Whether it really
*is certain though* ... if this [the std] were a law!

Ok, I reckon the meaning *is clear* [gulp] in this case.

> Also, the return value is a pointer into the original string (or NULL of
> course).

Jeez, I really want to be the last one to want to play *the pedantic card on
c.l.c* (surely, c.std.c is where *certain types* should 'go play'), but the
std says ...

>>The strtok function returns a pointer to the first character of a token,
>>or a null pointer if there is no token.

Now, it's late [here] and I've not bothered to parse *all* the previous
paras in the std to see if there's a case for categorically stating that
'token', in this context, *is* necessaraily a member of the set of things in
the set of inputs to strtok(). But, if there's not a case, then "returns a
pointer to the first character of a token" doesn't, I think, preclude strtok
returning a pointer into some local [or any other] buffer, rather than the
one encoding the original string [the input] ... just that [perhaps], at the
time, the semantics of what token it is pointing to tallys with what its
input is?

As to the answer to this ... I actually don't give much of a damn [*a ****
actually], I'm more of a computational linguist these days, and it's *the
language* that interests me mostly now [my X3J11 days are a distant
memory] - and how, something that often appears at first sight reasonably
clear, can, in actual fact, be anything but! However, I'd rather ... than
be a pedant about it all now.


--
===============================================================
In an attempt to reduce ?unwanted noise? on the ?signal? ...

Disclaimer:

Any comment/code I contribute might =NOT= be 100% portable, nor
semantically correct [read - ?not 100% pedantically correct?].
I don?t care too much about that though, and I reckon it?s the
same with most ?visitors? here. However, rest assured that any
?essential? (?) corrections WILL almost certainly appear v.soon
[read - ?to add noise as they see fit, a pedant will be along
shortly?].

WARNINGS: Always read the label. No beside-the-point minutiae
filter supplied. Keep away from children. Do not ignite.
===============================================================


.