home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- 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
- From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
- Subject: Re: definition of strncpy stupid?
- Message-ID: <1992Dec23.175009.7703@cs.tu-berlin.de>
- Sender: news@cs.tu-berlin.de
- Organization: Techn. University of Berlin, Germany
- 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>
- Date: Wed, 23 Dec 1992 17:50:09 GMT
- Lines: 22
-
- msb@sq.sq.com (Mark Brader) writes:
- > If you say
- > if (strlen (src) >= n) {
- > memcpy (dest, src, n-1);
- > dest[n-1] = '\0';
- > } else
- > strcpy (dest, src);
- > then you are taking an extra pass through the source string, which is
- > inefficient if it is long.
-
- And `src' had better be '\0'-terminated, or strlen may give you
- a memory access error. I found the otherwise little used memchr()
- function to come in handy with this.
-
- Here is a sixth way:
-
- *dest = 0;
- strncat(dest, src, n-1);
-
- Frohe Weihnachten,
-
- Jutta (jutta@cs.tu-berlin.de)
-