C++ strcpy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strcpy() copies a whole C-string into another array, including the ending '\0'. After the call, dest holds the same text as source.

Both arguments are required. dest must already be large enough for every character plus the '\0'. strcpy() does not grow the array, and the two arrays must not overlap. Include <cstring>.

Syntax

The first argument is where the copy goes. The second argument is the text you copy.

C++
strcpy(dest, "Hello");
CallWhat it does
strcpy(dest, "Hello")Copies Hello, including the ending null, into dest.
Library declarations
C++
char* strcpy(char* destination, const char* source);

Parameters

ParameterDescription
destRequired. The array you copy into. It must already be large enough.
sourceRequired. The C-string you copy from. It is not changed.

Return Value

Return ValueDescription
destThe same pointer you passed in, now holding the copy.

C++ Examples

Example 1 — Basic strcpy()

Copy a greeting into a char array.

C++
#include <iostream>
#include <cstring>

int main() {
    char dest[50];
    const char* src = "Hello, C++!";

    strcpy(dest, src);

    std::cout << dest << '\n';
    return 0;
}

How It Works

  1. dest is a writable buffer with room for the text.
  2. strcpy copies characters from src, then '\0'.
  3. Printing dest shows the copied string.

Example 2 — Change a Character After Copy

The destination is a separate copy you can edit.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* src = "Take the test.";
    char dest[40];

    strcpy(dest, src);
    dest[0] = 'M';

    std::cout << "src:  " << src << '\n';
    std::cout << "dest: " << dest << '\n';
    return 0;
}

How It Works

  1. Copy creates an independent buffer.
  2. Changing dest[0] does not change src.
  3. Literal source strings must not be modified in place.

Example 3 — Safer Style with std::string

Same idea without sizing a buffer yourself.

C++
#include <iostream>
#include <string>

int main() {
    std::string src = "Hello, C++!";
    std::string dest = src;

    std::cout << dest << '\n';
    return 0;
}

How It Works

  1. Assignment copies the text for you.
  2. No manual '\0' or buffer length math.
  3. Learn strcpy for C APIs; prefer std::string in new C++.

Frequently Asked Questions

strcpy() copies a whole C-string into another array, including the ending null character.
dest is the array you copy into. source is the C-string you copy from.
The same pointer as dest, now holding the copied text.
At least the length of source plus one byte for the ending null. strcpy() does not grow the array.
No. Overlapping the two arrays is not allowed.

Next: strcspn()

Count characters until one from a reject set appears.

strcspn() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful