home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / c / 3234 < prev    next >
Encoding:
Text File  |  1992-12-21  |  1.5 KB  |  44 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!malgudi.oar.net!news.ans.net!cmcl2!panix!spencer
  3. From: spencer@panix.com (David Spencer)
  4. Subject: Re: definition of strNcpy
  5. Message-ID: <1992Dec18.134739.484@panix.com>
  6. Date: Fri, 18 Dec 1992 13:47:39 GMT
  7. References: <1992Dec18.015228.6094@dms.agames.com>
  8. Organization: PANIX Public Access Unix, NYC
  9. Lines: 33
  10.  
  11. lipson@dms.agames.com (Pete Lipson) writes:
  12.  
  13. >  if you say:   strncpy(dst,"abcdef",4)
  14.  
  15. > does the standard really say you get five characters appended to the
  16. >dst (the chars 'a' 'b' 'c' 'd' AND the '\0') ???
  17. >  the man-page says it moves AT MOST the count (4 in this case) characters
  18. >but it ALSO says it returns a pointer to a NULL-TERMINATED result.
  19.  
  20. Only if it hits a null before it copies n chars:
  21.  
  22. 7.11.2.4:
  23.  
  24. "The strncpy function copies not more than n characters (characters
  25. that follow a null character are not copied) from the array pointed to
  26. by s2 to the array pointed to by s1. [134]"
  27.  
  28. "[134] Thus, if there is no null character in the first n characters
  29. that can end up in the array pointed to by s2, the result will not be
  30. null-terminated."
  31.  
  32. I suspect you are getting 4 chars from s2 and the '\0' is already in
  33. s1.
  34.  
  35. BTW, the standard also says:
  36.  
  37. "If the array pointed to by s2 is a string that is shorter than n
  38. characters, null characters are appended to the copy in the array
  39. pointed to by s1, until n characters in all have been written"
  40.  
  41. I wonder how many implementations do this?
  42.  
  43. dhs   spencer@panix.com
  44.