String copy (strcpy) and a memory copy (memcpy)
What is the difference between a string copy (strcpy) and a memory copy (memcpy)?
When should be the best time to use one over the other?
What is the difference between a string copy (strcpy) and a memory copy (memcpy)?
When should be the best time to use one over the other?
Strcpy (string copy) copies all the information associated with a certain variable of string.
Memcpy (memory copy) copies a block of information specified in the last argument of the function.
Following is the code for MEMCPY
#include <stdio.h>
#include <string.h>
#include <iostream.h>
void main ()
{
char str1[]="Sample text to copy";
char str2[40];
char str3[40];
memcpy (str2, str1, strlen(str1)+1);
memcpy (str3,"copying done",13);
cout<< "str1: " <<str1<< “ nstr2: ” <<str2<< “nstr3: ”<< str3;
}
the output is the picture attached..
———–strcpy on the other hand provides the same output but just removing the last argument in memcpy…
strcpy (str1, str2) > given that str1 is destination of the copied text and str2 is the source of text…
you can use whatever code you find it easy with and makes you more comfortable..