Most of these two functions usually use strcpy () but ignore memcpy (). They all copy content from the buffer zone.
Byte A [4]; // Value Type assigned to each byte
A [0] = 0;
A [1] = 1;
A [2] = 0;
A [3] = 1;
Byte c1 [4];
Memcpy (C1, A, sizeof (byte) * 4 );
Byte c2 [4];
Strcpy (char *) (byte *) C2, (char *) (byte *) );
Only memcpy () is correct, so compare them with their function prototype:
Char * strcpy (char * strdest, const char * strsource );
Void * memcpy (void * DEST, const void * SRC, size_t count );
We can see that strncpy () is the string to be processed (in case of zero termination), memcpy () is to process a buffer (void * type), and our content contains numbers 0, the number 0 represents the number of the string ending character '\ 0'. The string copy operation ends when it reaches 0. Therefore, if the buffer to be copied is non-string, use memcpy () as far as possible to avoid errors.
-------------------------------------------------------------------------
Void * memcpy (void * pvto, const void * pvfrom, size_t size)
{
Assert (pvto! = NULL) & (pvfrom! = NULL); // Use assertions
Byte * pbto = (byte *) pvto; // prevents the pvto address from being changed
Byte * pbfrom = (byte *) pvfrom; // prevents the pvfrom address from being changed
While (size --> 0)
* Pbto ++ = * pbfrom ++;
Return pvto;
}
Char * strcpy (char * strdest, const char * strsrc)
{
Assert (strdest! = NULL) & (strsrc! = NULL ));
Char * address = strdest;
While (* strdest ++ = * strsrc ++ )! = '')
Continue;
Return address;
}