C++ strcpy()

The strcpy() function in C++ copies a character string from source to destination.

strcpy() prototype

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

The strcpy() function takes two arguments: dest and src. It copies the character string pointed to by src to the memory location pointed to by dest. The null terminating character is also copied.

The behaviour is undefined if:

  • The memory allocated for dest pointer is not large enough.
  • The strings overlap.

It is defined in <cstring> header file.

strcpy() Parameters

  • dest: Pointer to a character array where the contents are copied to.
  • src: Pointer to a character array where the contents are copied from.

strcpy() Return value

The strcpy() function returns dest, the pointer to the destination.

Example: How strcpy() function works

#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    char src[] = "Hello Programmers.";
    
    /* Large enough to store content of src */
    char dest[20];
    
    strcpy(dest,src);
    cout << dest;
    return 0;
}

When you run the program, the output will be:

Hello Programmers.