home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19252 < prev    next >
Encoding:
Text File  |  1993-01-05  |  2.9 KB  |  76 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!nntp.Stanford.EDU!dkeisen
  3. From: dkeisen@leland.Stanford.EDU (Dave Eisen)
  4. Subject: Re: Problem with string processing.
  5. Message-ID: <1993Jan5.161120.1423@leland.Stanford.EDU>
  6. Sender: ?@leland.Stanford.EDU
  7. Organization: Sequoia Peripherals, Inc.
  8. Date: Tue, 5 Jan 93 16:11:20 GMT
  9. Lines: 65
  10.  
  11. In article <MCGLK.93Jan5005833@yang.cpac.washington.edu> mcglk@cpac.washington.edu (Ken McGlothlen) writes:
  12. >Roy M. Silvernail (roy%cybrspc@cs.umn.edu) writes:
  13. >| [regarding the C statment:  while (*ptr++ = *str++);  ]
  14. >|
  15. >| (nice touch, BTW) How does this differ from 'strcpy(ptr, str)' besides the
  16. >| function call overhead?
  17. >It doesn't.  In fact, that's often how strcpy() is implemented.
  18.  
  19. In the example that Roy took this from, there is a big difference.
  20. After the loop above, ptr points to the character one past the
  21. trailing '\0' of the string. This is convenient because the function
  22. it was taken from appended a character to the end of the string
  23. and used this pointer to do so.
  24.  
  25. The loop is equivalent to:
  26.  
  27.    strcpy (ptr, str);
  28.    ptr += strlen (str) + 1;
  29.  
  30. >| Assuming a large enough string for the append, and low-high byte ordering,
  31. >| would this work?
  32. >|
  33. >| strcat(str,(char *)&ch);
  34.  
  35. >Well, it would work *sometimes*.  Remember that strcat() expects two pointers
  36. >to ZERO-TERMINATED strings.  If what was stored in ch was immediately followed
  37. >by a zero byte ('\0'), then it *would* work.  But ch is stored wherever the
  38. >compiler wants to store it, and it could be followed by *anything*, which means
  39. >that you could get a heck of a lot more characters on the end of str than you
  40. >bargained for.
  41.  
  42. I think Roy was being cute here. I can never keep track of what 
  43. low-high byte order means or what different endianness means,
  44. but I think what he is saying is that if ch is an int formed
  45. by widening the character chr, ch is say 4 bytes that look
  46. like:
  47.  
  48.    chr  '\0'   '\0'   '\0'
  49.  
  50. so the strcat would work because the byte containing the value
  51. of ch *is* followed by '\0'.
  52.  
  53. It takes more than a byte order assumption to get this to work.
  54. First of all, there is no rule that states an int is bigger
  55. than a char; if they are the same size, your objection is
  56. completely valid. And if chars happen to be the same as signed
  57. chars, the widening from char to int must preserve value (using
  58. ANSI semantics) so if the char happens to be negative (on a two's
  59. complement system), the int ch looks like:
  60.  
  61.    chr   FFFF    FFFF   FFFF
  62.  
  63. and the strcat doesn't work either.
  64.  
  65. So Roy's trick, if I am understanding him correctly, will work
  66. fine on a system where a number of assumptions are correct. I'll
  67. let Roy defend the usage of his trick from those who would 
  68. argue it is not good style.
  69.  
  70.  
  71. -- 
  72. Dave Eisen                               Sequoia Peripherals: (415) 967-5644
  73. dkeisen@leland.Stanford.EDU              Home:                (415) 321-5154
  74.        There's something in my library to offend everybody. 
  75.           --- Washington Coalition Against Censorship
  76.