strcpy

Barry Margolin barmar at think.COM
Fri Mar 25 02:26:28 AEST 1988


In article <810 at cresswell.quintus.UUCP> ok at quintus.UUCP (Richard A. O'Keefe) writes:
>In article <545 at anuck.UUCP>, jrl at anuck.UUCP (j.r.lupien) writes:
>> From article <793 at cresswell.quintus.UUCP>, by ok at quintus.UUCP (Richard A. O'Keefe):
>> > The UNIX manuals say of strcpy(s1, s2) that it
>> > 	"copies s2 to s1, stopping after the null character has been copied."
>> > While they doesn't strictly speaking say anything about the order in which
>> > the other characters are copied, they _do_ say that the NUL character must
>> > be copied last, so 
>> Stopping after something occurs, as with "after the NULL has been copied"
>> does NOT equate, as you go on to assume, to "nothing will be done after
>> the NULL[*] is copied. The function will return immediately."
>
>That's not what I assumed.  The function could well compute factorial 5000.
>So what?  The manual says that COPYING stops after the NUL character has
>been copied.  So whatever strcpy does after copying NUL, either it doesn't
>copy any part of s2 to s1, or the manual entry is just plain wrong (which
>would not be unprecedented).  The point of my message was that AT&T
>documentation provides some warrant for expecting a left-to-right order
>rather than some other order.

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.  Programmers accustomed
to some other programming languages might have expected all the
declared contents of the string to be copied, and this phrase serves
as a reminder that string functions don't know about declared array
dimensions, all they know is that '\0' ends a string.

Barry Margolin
Thinking Machines Corp.

barmar at think.com
uunet!think!barmar



More information about the Comp.lang.c mailing list