strcpy

Dave Sill dsill at NSWC-OAS.arpa
Sat Mar 26 00:39:55 AEST 1988


In article <18488 at think.UUCP> Barry Margolin <barmar at think.COM> writes:
>Will you guys stop playing word games, and think about what that
>sentence was really intended to mean?  I think the point of the
>"stopping after the NUL" phrase is that it doesn't copy any characters
>after the NUL.  Thus, if you have
>
>	char [10] dest, source;
>	strcpy (source, "abcdefghi");
>	strcpy (dest, "123456789");
>	source [3] = '\0';
>	strcpy (dest, source);
>
>the resulting contents of dest will be
>
>	'a' 'b' 'c' '\0' '5' '6' '7' '7' '9' '\0'
>
>i.e. the last six characters are not affected.

I don't think that that's guaranteed, or even implied by that
sentence.  I would expect the contents of `dest' to be:

	'a' 'b' 'c' '\0'  ?   ?   ?   ?   ?   ?

where `?' may or may not be the same character that was in that
position before the call to strcpy.  I could imagine an implementation
that would null-out the destination string if it was longer than the
source.  ANSI describes `strcpy' a little differently:

  "The `strcpy' function copies the string pointed to by `s2'
   (including the terminating null character) into the array pointed
   to by `s1'.  If copying takes place between objects that overlap,
   the behavior is undefined."

There is nothing said about the order in which the copying takes
place, or the contents of the destination string past the null
character.

=========
The opinions expressed above are mine.

"The wretched reflect either too much or too little."
					-- Publilius Syrus



More information about the Comp.lang.c mailing list