You are here:

C++/cpp doubt


What is the difference between the strcpy and memcpy

Well this is more of a C question than a C++ question.

As an aside note that cpp does not necessarily mean C++ - cpp is also a program on UN*X and similar systems that handles C/C++ preprocessing (i.e. cpp is short for C Pre-Processor). Just so you know.

I say this is more of a C question because strcpy and memcpy are part of the C++ standard library that C++ inherited from C.

Both strcpy and memcpy handle copying chunks of memory specified by pointers from one place to another. The difference is how they do it. The strcpy function is intended for char arrays that represent C-style strings - i.e. zero terminated arrays of char, hence the str prefix. However memcpy is intended for any contiguous chunks of memory. This becomes obvious from their respective signatures:

   char * strcpy( char * dest, char const * src );
   void * memcpy( void * dest, void const * src, size_t n );

Both copy from the src to dest. However, note that whereas memcpy has a third parameter that specifies how many chars to copy strcpy does not - it continues copying up to and including the first zero valued char from src. Note also that whereas strcpy specifies char arrays (char pointers) and returns a pointer to the copied C-string (i.e. a pointer to src), memcpy specifies void * values and returns a pointer to the dest void pointer - so can be passed pointers to anything, but treats them as sequences of char (i.e. char arrays).

There is a variation of strcpy called strncpy that copies n characters from src to dest and its signature looks similar that of memcpy:

   char * strncpy( char * dest, char const * src, size_t n );

However, strncpy is _still_ for use with zero-terminated arrays of char (i.e. C-strings), so still specifies src and dest in terms of pointers to (zero terminated) char arrays. It copies at most n characters from dest to src, and like strcpy it returns a pointer to src. If src has fewer than n characters then strncpy pads characters at dest with '\0' character values.

Below are the man pages from a handily Linux session I have to hand for strcpy, strncpy and, following these, memcpy et al.

   man strcpy

Gives the following description:

         strcpy, strncpy - copy a string

         #include <string.h>

         char *strcpy(char *dest, const char *src);

         char *strncpy(char *dest, const char *src, size_t n);

         The  strcpy()  function copies the string pointed to by src, including the terminating null byte (\0), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.

         The strncpy() function is similar, except that at most n bytes of src are copied.
         Warning: If there is no null byte among the first n bytes of src, the string placed in dest
         will not be null terminated.

         If the length of src is less than n, strncpy() pads the remainder of dest with null bytes.

         A simple implementation of strncpy() might be:

         strncpy(char *dest, const char *src, size_t n){
         size_t i;

         for (i = 0 ; i < n && src[i] != \0 ; i++)
         dest[i] = src[i];
         for ( ; i < n ; i++)
         dest[i] = \0;

         return dest;

         The strcpy() and strncpy() functions return a pointer to the destination string dest."


   man memcpy
Gave this description:

         memcpy - copy memory area

         #include <string.h>

         void *memcpy(void *dest, const void *src, size_t n);

         The  memcpy()  function  copies n bytes from memory area src to memory area dest.  The memory areas should not overlap.  Use memmove(3) if the  memory areas do overlap.

         The memcpy() function returns a pointer to dest."

I should note and heed the small print and warnings in these descriptions if I were you, i.e. that strncpy does not necessarily terminate the C-string pointed to by dest and that the memory areas specified by src and dest for memcpy must be separate and not overlap (use memmove for such cases). Oh, and for ISO/ANSI standard C++ we include <cstring> rather than <string.h>.

Note that if you do not have a system with descriptions of such standard library functions then you can find them easily enough online using a search engine - try entering the name of the function you are interested in.

Most Linux and UN*X systems should have the manual (man) pages installed for such functions and if not then you may be able to install them - e.g. by selecting the appropriate packages to install on a Linux system from a package manager application..

For Microsoft compilers, check the documentation that comes with the Visual C++ product (a version of the MSDN library for local use is usually included or available for 'recent' releases) - or check out the MSDN library online at

Please refer to documentation for such standard functions before asking similar questions in the future as you will probably find the information you are looking for by reading carefully such descriptions, and you will find it a lot quicker! Using me as a reference manual is something of a waste of both our time <g>. However, if after carefully reading such descriptions you still have queries then please ask a question - however be aware that I will probably start off by reviewing similar if not the same descriptions for such standard functions...

Hope this help.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2016 All rights reserved.