home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19549 < prev    next >
Encoding:
Internet Message Format  |  1993-01-11  |  2.4 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!think.com!ames!agate!stanford.edu!kronos.arc.nasa.gov!butch!netcomsv!netcomsv!ulogic!hartman
  2. From: hartman@ulogic.UUCP (Richard M. Hartman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with string processing.
  5. Message-ID: <846@ulogic.UUCP>
  6. Date: 11 Jan 93 18:39:38 GMT
  7. References: <1idn10INNpvg@calamari.hi.com> <1993Jan11.060615.2180@eagercon.com>
  8. Organization: negligable
  9. Lines: 46
  10.  
  11. In article <1993Jan11.060615.2180@eagercon.com> eager@eagercon.com writes:
  12. >In article 1idn10INNpvg@calamari.hi.com, rogers@calamari.hi.com (Andrew Rogers) writes:
  13. >>In article <Ts3TwB1w165w@wozzle.linet.org> alane@wozzle.linet.org (J. Alan Eldridge) writes:
  14. >>>>         char tmp[2] = {0};
  15. >>>>  
  16. >>>>         *tmp = ch;
  17. >>>>         strcat(str, tmp);
  18. >>>
  19. >>>tmp[0] has a 0 in it but tmp[1] is undefined. You are not creating
  20. >>>a nul-terminated string here.
  21. >>
  22. >>tmp[1] most certainly does have a 0 in it; if the initialization list for
  23. >>an array contains fewer elements than the array itself, then the remaining
  24. >>array elements are initialized to 0.  Check the ANSI spec, or the relevant
  25. >>section in K&R.
  26. >
  27. >Well, the relevent section is 3.5.7, which says, in part "If an object that 
  28. >has automatic storage duration is not initialized explicitly, its value is
  29. >indeterminate."  Lacking a 'static' declaration and being within a function
  30. >means that tmp has automatic storage.  Only the first element is initialized
  31. >to zero, tmp[1] is undefined.
  32. >
  33. >For static storage objects, all uninitialize elements get binary zero values.
  34.  
  35. tmp[0] and tmp[1] are NOT separate 'char's, they are a part of an
  36. array, the initialization of which is governed by it's own set
  37. of rules.
  38.  
  39. In K&R (the original, sorry) section 4.9, near the end, regarding 
  40. array initialization it says "If there are fewer initializer than 
  41. the specified size, the others will be zero."
  42.  
  43. I am certain that this rule has been carried up.  I do not have
  44. the ANSI document, however, to check.  The equivilant section for
  45. the C++ ARM, "8.4.1 Aggregates" where it says "If there are fewer 
  46. initializers in the list than there are members of the aggregate, 
  47. then the aggregate is padded with zeros of the appropriate types."  
  48. I would expect that the ANSI C doc is closer to this than the 
  49. original K&R.
  50.  
  51.         -Richard Hartman
  52.         hartman@uLogic.COM
  53.  
  54. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  55. Disco isn't dead...            ...it's just in witness protection!
  56.  
  57.