Re: strtok/strtok_r woes
From: Jack Klein (jackklein_at_spamcop.net)
Date: 01/25/05
- Next message: Richard Harter: "Re: convert a list to tree"
- Previous message: Old Wolf: "Re: cast-as-lvalue (Thank You)"
- In reply to: nimmi_srivastav_at_yahoo.com: "Re: strtok/strtok_r woes"
- Next in thread: Jonathan Burd: "Re: strtok/strtok_r woes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Richard Harter: "Re: convert a list to tree"
- Previous message: Old Wolf: "Re: cast-as-lvalue (Thank You)"
- In reply to: nimmi_srivastav_at_yahoo.com: "Re: strtok/strtok_r woes"
- Next in thread: Jonathan Burd: "Re: strtok/strtok_r woes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|