strcpy

00704a-Liber nevin1 at ihlpf.ATT.COM
Tue Apr 5 07:40:42 AEST 1988


In article <836 at cresswell.quintus.UUCP> ok at quintus.UUCP (Richard A. O'Keefe) writes:

>Questions like "what happens to the rest of the destination" and "what
>happens if the two areas overlap" are so important that the answers
>SHOULD be part of the description of strcpy(). It is extremely useful to
>have a function which can safely be used to move part of a character
>array towards its origin.

I agree that it is useful to have a function which can safely move strings
with overlapping characters.  That is what memmove() is for.

BTW, the answer to "what happens to the rest of the destination" in
strcpy() would be is that it is unaffected, since there is no way of conveying
what is meant by "the rest of the destination" to a function call; ie, how
can strcpy() tell the difference between an exact fit and an inexact fit?
It can't.  And the answer to "what happens if the two areas overlap" is
found directly in the standard:

	"If copying takes place between objects that overlap, the behavior is
	undefined."

You may not like the answer, but the standard answers the question just the
same.

>If the memcpy() question was solved by adding a memmove(), is there
>also a strmove() in the current dpANS draft?

strmove() is not needed since it is just a very special case of memmove().

In order to copy possibly overlapping strings, you need to know the length
of the source string.  Therefore, give a source string s2 (char *s2) and a
destination string s1 (char *s1):

	(char *)memmove((void *)s1, (void *)s2, strlen(s2) + (size_t)1)

will accomplish that you would want a strmove() to do.
-- 
 _ __			NEVIN J. LIBER	..!ihnp4!ihlpf!nevin1	(312) 510-6194
' )  )				"The secret compartment of my ring I fill
 /  / _ , __o  ____		 with an Underdog super-energy pill."
/  (_</_\/ <__/ / <_	These are solely MY opinions, not AT&T's, blah blah blah



More information about the Comp.lang.c mailing list