home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18834 < prev    next >
Encoding:
Text File  |  1992-12-23  |  1.1 KB  |  34 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!uwm.edu!spool.mu.edu!yale.edu!ira.uka.de!Sirius.dfn.de!mailgzrz.TU-Berlin.DE!cs.tu-berlin.de!jutta
  3. From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
  4. Subject: Re: definition of strncpy stupid?
  5. Message-ID: <1992Dec23.175009.7703@cs.tu-berlin.de>
  6. Sender: news@cs.tu-berlin.de
  7. Organization: Techn. University of Berlin, Germany
  8. References: <1992Dec8.181543.15339@dnbf01.bram.cdx.mot.com> <1992Dec9.144743.1863@rti.rti.org> <16298@goanna.cs.rmit.oz.au> <1992Dec23.100054.2939@sq.sq.com>
  9. Date: Wed, 23 Dec 1992 17:50:09 GMT
  10. Lines: 22
  11.  
  12. msb@sq.sq.com (Mark Brader) writes:
  13. > If you say
  14. >    if (strlen (src) >= n) {
  15. >        memcpy (dest, src, n-1);
  16. >        dest[n-1] = '\0';
  17. >    } else
  18. >        strcpy (dest, src);
  19. > then you are taking an extra pass through the source string, which is
  20. > inefficient if it is long.
  21.  
  22. And `src' had better be '\0'-terminated, or strlen may give you
  23. a memory access error.  I found the otherwise little used memchr()
  24. function to come in handy with this.
  25.  
  26. Here is a sixth way:
  27.  
  28.     *dest = 0;
  29.     strncat(dest, src, n-1);
  30.  
  31. Frohe Weihnachten,
  32.  
  33. Jutta (jutta@cs.tu-berlin.de)
  34.