C++ strcpy Function: Usage Guide

The strcpy function in C++ is used to copy one string to another string. Its basic syntax is as follows:

char* strcpy(char* destination, const char* source);

The destination pointer is the pointer for the target string, while the source pointer is for the source string. The strcpy function copies the content of the source string to the target string until it encounters the end symbol ‘\0’ of the source string. It should be noted that the target string must be large enough to accommodate the content of the source string, otherwise a buffer overflow may occur.

Example Usage:

char source[] = "Hello, world!";
char destination[20];
strcpy(destination, source);
cout << destination; // 输出为:Hello, world!

To prevent buffer overflow issues, you can use the safer strcpy_s function, which is used in a similar way.

char source[] = "Hello, world!";
char destination[20];
strcpy_s(destination, sizeof(destination), source);
cout << destination; // 输出为:Hello, world!
bannerAds