std::strcpy(3)
hiii bestie π welcome to the std::strcpy(3) page from the C++ Standard Libary β yes, the header spells it like that, and no, weβre not fixing it, itβs iconic π
. Manual name folded in, vibe check passed, letβs get into the string-copying tea.
β¨ NAME β¨
std::strcpy - std::strcpy. Self-titled icon behavior, honestly π.
π¦ Synopsis π¦
Defined in header <cstring>
char* strcpy( char* dest, const char* src );
So strcpy copies the character string pointed to by src, including the null terminator, into the character array whose first element is pointed to by dest.
The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap. No cap, those are the danger zones π.
πΈ Parameters πΈ
destβ pointer to the character array to write to.srcβ pointer to the null-terminated byte string to copy from.
π Return value π
dest. Thatβs it. She gives you back the destination pointer, periodt β¨.
π Example π
// Run this code
#include <cstring>
#include <iostream>
#include <memory>
int main()
{
const char* src = "Take the test.";
// src[0] = 'M'; // can't modify string literal
auto dst = std::make_unique<char[]>(std::strlen(src) + 1); // +1
for null terminator
std::strcpy(dst.get(), src);
dst[0] = 'M';
std::cout << src << '\n' << dst.get() << '\n';
}
Lowkey obsessed with this little demo π«Ά: it makes a std::unique_ptr<char[]> sized for the string plus the null terminator, copies with std::strcpy, then changes the first character of the copy. The commented line is literally // src[0] = 'M'; // can't modify string literal β string literals said βnot todayβ π.
π Output π
Take the test.
Make the test.
It prints both, and the second one starts with M while the original still starts with T β main character energy for sure π.
π See also π
strncpyβ copies a certain amount of characters from one string to another (function)memcpyβ copies one buffer to another (function)- C documentation for
strcpy
thatβs the page, bestie π«Ά β source/version: http://cppreference.com, date: 2024.06.10, page: std::strcpy(3). go copy responsibly π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms