Home C++ Strings Functions strcpy() C++ strcpy() Function 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.
Call What it does strcpy(dest, "Hello")Copies Hello, including the ending null, into dest.
Library declarations char* strcpy(char* destination, const char* source); Parameters Parameter Description dest Required. The array you copy into. It must already be large enough. source Required. The C-string you copy from. It is not changed.
Return Value Return Value Description dest The same pointer you passed in, now holding the copy.
C++ Examples 1. Basic copy 2. Edit after copy 3. vs std::string Example 1 — Basic strcpy() Copy a greeting into a char array.
#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 dest is a writable buffer with room for the text.strcpy copies characters from src, then '\0'.Printing dest shows the copied string. Example 2 — Change a Character After Copy The destination is a separate copy you can edit.
#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;
} src: Take the test.
dest: Make the test. How It Works Copy creates an independent buffer. Changing dest[0] does not change src. Literal source strings must not be modified in place. Example 3 — Safer Style with std::string Same idea without sizing a buffer yourself.
#include <iostream>
#include <string>
int main() {
std::string src = "Hello, C++!";
std::string dest = src;
std::cout << dest << '\n';
return 0;
} How It Works Assignment copies the text for you. No manual '\0' or buffer length math. Learn strcpy for C APIs; prefer std::string in new C++. Frequently Asked Questions What does strcpy() do in C++? strcpy() copies a whole C-string into another array, including the ending null character.
What are the parameters of strcpy()? dest is the array you copy into. source is the C-string you copy from.
What does strcpy() return? The same pointer as dest, now holding the copied text.
How big does dest need to be? At least the length of source plus one byte for the ending null. strcpy() does not grow the array.
Can source and dest overlap? No. Overlapping the two arrays is not allowed.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion