home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / c / 3040 < prev    next >
Encoding:
Text File  |  1992-11-19  |  2.2 KB  |  57 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!haven.umd.edu!decuac!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond
  3. From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
  4. Subject: Re: idle query on strtok()
  5. Message-ID: <1992Nov20.012856.14506@nntpd.lkg.dec.com>
  6. Sender: usenet@nntpd.lkg.dec.com (USENET News System)
  7. Reply-To: diamond@jit.dec.com (Norman Diamond)
  8. Organization: Digital Equipment Corporation Japan , Tokyo
  9. References: <722063993snx@grendel.demon.co.uk>
  10. Date: Fri, 20 Nov 1992 01:28:56 GMT
  11. Lines: 44
  12.  
  13. In article <722063993snx@grendel.demon.co.uk> jes@grendel.demon.co.uk (Jim Segrave) writes:
  14. >Is strtok()'s behaviour defined for tokens already taken from a string?
  15.  
  16. strtok() only steps on separators, so the tokens remain unchanged.
  17.  
  18. >    char zoe[] = "aaa bbb";
  19. >    char *first, *second;
  20. >    first = strtok (zoe, " "); 
  21. >    second = strtok (NULL, " ");
  22. >    printf ("%s\n%s\n", first, second);
  23. >I expect the output:
  24. >    aaa
  25. >    bbb
  26.  
  27. Perfect example.
  28.  
  29. >Can I *portably* change the contents of a token returned by strtok
  30. >without affecting the behaviour of subsequent calls?
  31. >    strcpy (first, "!!!");        /* corrupt the first bit */
  32. >    second = strtok (NULL, " ");
  33. >    printf ("%s\n", second);
  34.  
  35. Yes.  strtok() is defined as keeping a pointer internally to the remaining
  36. portion of the string, from which its next search will continue.  The
  37. second result will still be bbb.
  38.  
  39. >What if I change the 'corrupt ...' line to
  40. >    memset (first, 0, strlen (first) + 1);
  41.  
  42. No change.  The first call to strtok() already set *(first + strlen (first))
  43. to 0, so you're just setting it to 0 again, as well as zeroing the token.
  44.  
  45. >Does strtok() promise that the tokens delivered will be untouched
  46. >by subsequent calls to strtok()?
  47.  
  48. Not explicitly, but still yes.  Library functions are defined (ideally
  49. anyway, since some aren't defined very clearly :-) to have certain effects
  50. on valid arguments, and are not allowed to go stepping on other parts of
  51. the program or have other random behavior unless you pass invalid arguments
  52. or you have otherwise violated the conditions.
  53. --
  54. Norman Diamond       diamond@jit081.enet.dec.com
  55. If this were the company's opinion, I wouldn't be allowed to post it.
  56. "It's been a lovely recession."
  57.