strcpy in C++

Last Updated : 6 Aug, 2026

The strcpy() function is a standard library function in C++ used to copy one null-terminated string into another. It is declared in the <cstring> (or <string.h>) header file.

  • Copies the source string, including the null ('\0') terminator, to the destination string.
  • The destination array must have enough memory to store the copied string safely.
C++
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    // Strings Declared
    char str1[] = "Hello Geeks!";
    char str2[] = "GeeksforGeeks";

    char str3[40];
    char str4[40];

    char str5[] = "GfG";

    // String copy used
    strcpy(str2, str1);
    strcpy(str3, "Copy successful");
    strcpy(str4, str5);

    // Strings Printed
    cout << "str1: " << str1 << "\nstr2: " << str2
         << "\nstr3: " << str3 << "\nstr4: " << str4;
  
    return 0;
}

Output
str1: Hello Geeks!
str2: Hello Geeks!
str3: Copy successful
str4: GfG

Syntax 

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

Parameters: This method accepts the following parameters

  • dest: Pointer to the destination array where the content is to be copied.
  • src: string which will be copied.

Return Value: After copying the source string to the destination string, the strcpy() function returns a pointer to the destination string.

Advantages

The strcpy() function is widely used for copying C-style strings because it is simple and efficient for basic string copy operations.

  • Simple and easy to use for copying one null-terminated string to another.
  • Efficient for copying complete strings when the destination buffer is large enough.
  • Returns a pointer to the destination string, allowing it to be used in expressions or function chaining.

Limitations

Despite its simplicity, strcpy() has some limitations that can lead to security and reliability issues if not used carefully.

  • Does not check the size of the destination array, which can lead to buffer overflow.
  • Copies the entire string, including the null ('\0') terminator, without any length restriction.
  • Works only with null-terminated C-style strings and cannot be used safely with non-null-terminated character arrays.
Comment