home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18340 < prev    next >
Encoding:
Text File  |  1992-12-14  |  2.6 KB  |  54 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!caen!hellgate.utah.edu!lanl!cochiti.lanl.gov!jlg
  3. From: jlg@cochiti.lanl.gov (J. Giles)
  4. Subject: Re: No, but strncat() is stupid (was: definition of strncpy stupid?)
  5. Message-ID: <1992Dec14.190936.1589@newshost.lanl.gov>
  6. Sender: news@newshost.lanl.gov
  7. Organization: Los Alamos National Laboratory
  8. References: <DAVIS.92Dec8113707@pacific.mps.ohio-state.edu>  <1992Dec10.175103.11502@newshost.lanl.gov> <1992Dec11.095325.1871@falcon.aamrl.wpafb.af.mil>
  9. Date: Mon, 14 Dec 1992 19:09:36 GMT
  10. Lines: 42
  11.  
  12. In article <1992Dec11.095325.1871@falcon.aamrl.wpafb.af.mil>, bkottmann@falcon.aamrl.wpafb.af.mil (Brett Kottmann) writes:
  13. |> In article <1992Dec10.175103.11502@newshost.lanl.gov>, jlg@cochiti.lanl.gov (J. Giles) writes:
  14. |> > I can't simply say 
  15. |> > 
  16. |> >    strncat(s1,s2,N);
  17. |> > 
  18. |> > This would *add* N characters to s1, not limit it's total length
  19. |> > to N.  In order to do what I want, I have to:
  20. |> > 
  21. |> >    strncat(s1, s2, N - strlen(s1));
  22. |> > 
  23. |> > Except, I can't do this either.  Previous operations on s1 may 
  24. |> > have left it without null termination - so strlen() won't work.
  25. |> 
  26. |>     It better have null termination.  strncat uses that to start storing
  27. |> s2.
  28.  
  29. You've missed my point.  If the length of `s1' is equal to `N', there is
  30. no room for null termination.  Therefore `s1' will not have it.  No matter,
  31. the amount I want strncat to move in that case it zero characters.  So,
  32. strncat() will not have to find any null termination of `s1'.  I was
  33. pointing out the same thing you were - only one step earlier.  Since
  34. `s1' may not be null terminated (it can't be if there's no room), then
  35. strlen() won't work on it.  Of course, you could pretend the allocated
  36. length was one character shorter than it actually was and insure that
  37. there was always room for a terminating null.  That still doesn't solve
  38. the problem that you have to do all the calculations yourself in order
  39. to make sure that you don't run past the end : you still have to do the
  40. redundant `N-strlen(s1)' expression.
  41.  
  42. Personally, I think that null termination is a bad idea anyway, and
  43. when I have to do character manipulation in C, I use the mem***()
  44. functions exclusively.  In fact, all the character variables I 
  45. declare are structs which explicitly carry the present length, the
  46. allocated length, and the pointer to the actual string.  I then use
  47. string manipulation functions of my own (which use the mem***() stuff)
  48. that automatically maintain the information in the struct.  This is
  49. almost *always* faster than the str***() or the strn***() functions
  50. and is certainly safer.
  51.  
  52. -- 
  53. J. Giles
  54.