• C

strcpy and memcpy... usages??

Hi guys,

   I have a question.. I have been looking at some in-house source codes and I noticed that to copy strings, sometimes strcpy is used and sometimes memcpy is used...
I can't differenciate when to use strcpy and memcpy..
can anybody explain WHEN and WHY in IT projects we would use 'strcpy' and/or 'memcpy' and does it not matter?

Thanks a lot...

Who is Participating?

Improve company productivity with a Business Account.Sign Up

BayoConnect With a Mentor Commented:

memcpy it´s more general funtion, strcpy only copy NULL terminated strings and memcpy can copy all types of arrays not only char.

to use memcpy you need especify the size of the array this avoid the null termination.

memcpy repeat the char if it´s not for all the long I.E.
memcpy(MyArray,0,sizeof(MyArray)); full all the array of 0´s

The only difference is that memcpy copies a given number of bytes, regardless of a zero
terminator in the block to be copied. 'strcpy' stops on each zero terminator.
Maybe 'memcpy' is used when strings like this are to be copied:
"string 1\0string 2\0string3\0\0" (double \0\0 at the end). Of course, the length of this strings
must be known before copying.
But the following lines are equivalent:

char *string1,*string2;
/* .... */
Hello Haho,  Snoegler answer your question.

In addition to that there are another important deference : If the two string ( Dest and Source ) are overlapped ( have an intersection in memory location ) memcpy will preduce an unpredictable results so that strcpy is more safe and sutable in most cases.

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

snoegler - I think you mean


If a zero occurs anywhere in the middle of a string then memcpy is likely to be used.

memcpy is also used to copy structures etc.

Motaz - memcpy is not guaranteed to work for overlapping strings either
(begin quote)
The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.
(end quote)

Answers2000, you're right. Of course i forgot '+1'.
BTW: Motaz mentioned this overlapping memory issue :)
What did overlapping mean snoegler ?
>> BTW: Motaz mentioned this overlapping memory issue :)

My point is that Motaz's original comment on overlapping is NOT correct.

Both strcpy and memcpy are NOT guaranteed to work on overlapping strings.

For memcpy, see the quote in my previous comment

For strcpy
(begin quote)
The behavior of strcpy is undefined if the source and destination strings overlap.
(end quote)

Motaz - I thought you should no what I we mean by overlapping as you brought it up, but point is that the destination memory block "overlaps" with the source block, example:

char someString[] = "Hello World" ;
char * src = someString + 5 ;
char * dest = someString ;

strcpy( dest, src ) ; /* May not work, undefined behaviour */


memcpy( dest, src, strlen(src)+1) ; /* Undefined behaviour too */

HahoAuthor Commented:
>>If a zero occurs anywhere in the middle of a string then memcpy is likely to
     be used.

But zeroes can happen a lot in normal string text, don't u guys agree??
And '\0' is not 0. So is the statement above correct...
My understanding is that ( after all the info) is that memcpy is good for strings that may contain '\0' (null terminator) but we should also be wary of zeroes???

to motaz: I am sorry but I rejected your answer as snoegler has given a very good answer and also the fact u asked what overlapping is when u yourself gave the answer.. :)
any other comments is welcome...
>> But zeroes can happen a lot in normal string text, don't u guys agree??

Zeroes (I mean char c = 0 ; ) don't occur in the middle of C style strings.  All the standard library string functions (strcpy, strcat, etc.) assume 0 occurs to mark the end of the string, and NOT in the middle of the string.  The mem** (memcpy etc) functions do handle 0's in the middle of a block of memory

>>And '\0' is not 0. So is the statement above correct...

'\0' is 0, try this program to prove it

#include <stdio.h>
int main( int argc, char * argv )
char c1 = 0 ;
char c2 = '\0' ;

if ( c1 == c2 )
 printf( "The same" ) ;
} else
 printf( "Different" ) ;
return 0 ;

The memxxx functions work for all binary data.  The strxxx functions work for a small but important subset of this data, ie, strings as defined by the C language.
HahoAuthor Commented:
Thanks Answer2000 ,
   I am a bit confused :(
So if I have a string
  strcpy( str1, "01234");
what will happen? In this case is the '0' a char = 0x00 or is it

I know what is overlapping but I want know it in snoegler view
>>So if I have a string
  strcpy( str1, "01234");
>>what will happen? In this case is the '0' a char = 0x00 or is it 0x30??

In this case it's 0x30


char c1 = '\0' ;
char c2 = '0' ;
char c3 = 0 ;

In this case c1 will contain 0x00
In this case c2 will contain 0x30
In this case c3 will contain 0x00

Characters like c1 and c3 CAN NOT occur in the middle of a string, because strcpy, strcat, etc. WILL NOT handle them.

Characters like c2 CAN occur in the middle of the string and strcpy, strcat, etc. WILL handle them.

If you read back by previous comments considering that by character 0 I mean 0x00 (the NUL character, ASCII value 0) they should make more sense.

HahoAuthor Commented:
Thanks , esp. to Answers2000 and also Sneogler who has taken great pains to explain the whole difference thing to me.. and I must say that I understand it well now... :)
Take care... until my next confused question ... ;)


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.