Re: strtok/strtok_r woes

From: Jack Klein (jackklein_at_spamcop.net)
Date: 01/25/05


Date: Mon, 24 Jan 2005 22:36:53 -0600

On 24 Jan 2005 18:37:09 -0800, nimmi_srivastav@yahoo.com wrote in
comp.lang.c:

> In the interest of "public disclosure" I am reproducing the coding
> sample that I see in my copy of "C: The Complete Reference"
>
> --NS
>
> #include "stdio.h" /* NS: Shouldn't this be <stdio.h>? */
> #include "string.h" /* NS: Shouldn't this be <string.h>? */

Yes, they both should. The #include "sometext" format is for
including source files, the #include <sometext> format is for
including standard headers, which need not be files.

> void main(void)

This of course is just plain undefined behavior.

> {
> char *p;
>
> p = strtok("The summer soldier, the sunshine patriot", " ");
> /* NS: Passing a constant string to strtok */

No, your comment is in correct. He is passing the address of a string
literal, but the type of a string literal in C is "array of char" and
NOT "array of const char". Attempting to modify a string literal in C
does indeed produce undefined behavior, but not because the characters
are const, just because the C standard specifically says so.

Because attempting to modify a string literal is undefined, compilers
are free to actually make them const, but are not required to do so.
No strictly conforming program can tell one way or the other.

> printf(p); /* NS: Isn't %s missing here? */

Not necessarily. If you pass a pointer to char to printf, assuming
that the pointer points to a valid string, printf will just output the
string. Unless of course the string happens to contain a conversion
specifier. If p points to "%s" for example, yet another incident of
undefined behavior occurs.

> do {
> p = strtok('\0', ", ");
> if(p) printf("|%s", p);
> } while(p);
> }

Let it never be said that Schildt lacks the talent to cram multiple
examples of undefined behavior in such a small piece of code.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: Opinions on this code please guys....
    ... to see if the string writes off the end of the buffer. ... there's no check for error (or even EOF) which is quite ... This is allso undefined behavior and stupid. ... Even the above is horrendously ineffiient and stupid. ...
    (comp.lang.cpp)
  • Re: code works
    ... You have no idea if argvexists or if it points to a string. ... And here you invoke undefined behavior the same as in your original ... I am receiving a lot of good help here ... school for spelling and now have a serious problem remembering how to spell ...
    (comp.lang.c)
  • Re: [C++] Need help with program please
    ... > out each of the words which make up that line of text (using arrays). ... This loop will run off the end of your string. ... calculating the length for each iteration is wasteful. ... So 'str' also gives undefined behavior. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Compiler difference
    ... String literals, by definition are not allowed to be ... Attempts to do so result in 'undefined behavior' ... This creates a pointer, but doesn't give it a value (it doesn't ... Your code had no diagnosable errors, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Function call evaluation order
    ... void f1 ... int i = 0; ... has.completely undefined behavior since i is modified twice between ...
    (microsoft.public.vc.language)