๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ strcpy-in-c
strcpy() in C - GeeksforGeeks
March 6, 2026 - It is a part of the C standard strings library. The strcpy function in C is used to copy a string, with syntax: strcpy(dest, src);, where dest is the destination array and src is the source string.
๐ŸŒ
Wikibooks
en.wikibooks.org โ€บ wiki โ€บ C_Programming โ€บ string.h โ€บ strcpy
C Programming/string.h/strcpy - Wikibooks, open books for an open world
Although the simple assignment ... it only copies the memory address of str1 into str2 but not the actual string. Both str1 and str2 would refer to the same memory block, and the allocated block that used to be pointed to by str2 would be lost. The assignment to str2[0] would either also modify str1, or it would cause an access ...
Discussions

c++ - Proper way to copy C strings - Stack Overflow
Many implementations have a "short ... short strings; in that case, there will be little or no overhead over using a C-style array. Access to individual characters is just as convenient as with a C-style array; in both cases, s[i] gives the character at position i as an lvalue. Copying becomes stringB ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How does the STL string library handle copying C strings into an std::string?
How are you making a std::string from value? The only safe ways are the constructors that communicate the size of, or the end of, the string std::string w{std::begin(value), std::end(value)}; std::string w{value,4}; (and trivial variants) More on reddit.com
๐ŸŒ r/Cplusplus
8
5
July 13, 2022
Safest way to copy a string?
One option is snprintf(dest,n,โ€œ%sโ€,src), but this will likely be a little slower due to the time needed to parse the format string. More on reddit.com
๐ŸŒ r/C_Programming
79
55
May 7, 2023
How do you convert vector of char to string in c++?
Simply use the std::strings range ctor: std::vector chars; std::string str{ chars.begin(), chars.end() }; Might I also point out that you can search the internet . More on reddit.com
๐ŸŒ r/cpp_questions
9
3
August 8, 2020
๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ library-function โ€บ string.h โ€บ strcpy
C strcpy() - C Standard Library
The strcpy() function also returns the copied string. The strcpy() function is defined in the string.h header file. #include <stdio.h> #include <string.h> int main() { char str1[20] = "C programming"; char str2[20]; // copying str1 to str2 strcpy(str2, str1); puts(str2); // C programming return 0; }
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ ref_string_strcpy.php
C string strcpy() Function
C Examples C Real-Life Examples ... str1); printf("%s\n", str2); Try it Yourself ยป ยท The strcpy() function copies data from one string into the memory of another string....
๐ŸŒ
DEV Community
dev.to โ€บ sjmulder โ€บ string-copy-in-c-38k9
String copy in C - DEV Community
August 12, 2023 - Instead, use strcpy_s() if available, strclpy(), or even snprintf() (snprintf(dst, sizeof(dst), "%s", src)). ... void strcpy_1(char *src, char *dst) { size_t len, i; len = strlen(src); for (i=0; i < len; i++) dst[i] = src[i]; dst[len] = '\0'; ...
๐ŸŒ
W3Schools
w3schools.com โ€บ cpp โ€บ ref_cstring_strcpy.asp
C++ cstring strcpy() Function
The strcpy() function copies data from one C-style string into the memory of another string.
๐ŸŒ
C For Dummies
c-for-dummies.com โ€บ blog
To Copy or to Duplicate a String | C For Dummies Blog
December 26, 2015 - The destinationโ€™s allocated space must be of the same size (or larger) than the source. The value returned is a pointer to the dst string. The strcpy() function duplicates string scr to the location specified by dst one character at a time until and including the null character, \0.
Find elsewhere
๐ŸŒ
Sternum IoT
sternumiot.com โ€บ home โ€บ strcpy and strncpy c functions โ€“ syntax, examples, and security best practices
strcpy and strncpy C Functions | Syntax, Examples & Security Best Practices | Sternum IoT
January 30, 2024 - It is included in the string.h header file and stands for โ€œstring copy.โ€ The primary objective of this function is to replicate a source string into a destination buffer while ensuring both strings are null-terminated. The strcpy() function ...
๐ŸŒ
Linux Man Pages
linux.die.net โ€บ man โ€บ 3 โ€บ strcpy
strcpy(3): copy string - Linux man page
The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated. If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c strcpy()
C strcpy() - Scaler Topics
July 14, 2024 - Here, string 1 is the source (original) ... change or modify the content of source string. The image below explains the working of the strcpy() in C....
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_ref_string.php
C string (string.h) Library Reference
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A ... The <string.h> library has many functions that allow you to perform tasks on strings.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.string.copy
String.Copy(String) Method (System) | Microsoft Learn
Creates a new instance of String with the same value as a specified String. public: static System::String ^ Copy(System::String ^ str);
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cstring โ€บ strcpy
strcpy - cstring
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
๐ŸŒ
CSE CGI Server
cgi.cse.unsw.edu.au โ€บ ~cs1511 โ€บ 26T1 โ€บ lab โ€บ 08 โ€บ questions
COMP1511 26T1 โ€” Week 08 Laboratory Problem Set
Your contains function will be called directly in marking. The main function is only to let you test your contains function. dcc list_contains.c -o list_contains ./list_contains How many strings in initial list?: 4 pepperoni ham basil capsicum Enter word to check contained: basil 1 ./list_contains How many strings in initial list?: 4 pepperoni ham basil capsicum Enter word to check contained: mozzarella 0 ./list_contains How many strings in initial list?: 4 chicken mushroom mushroom pizza-sauce Enter word to check contained: mushroom 1 ./list_contains How many strings in initial list?: 4 tomato bacon capsicum mushroom Enter word to check contained: pepperoni 0 ./list_contains How many strings in initial list?: 0 Enter word to check contained: tomato 0
๐ŸŒ
Michael Stapelberg
michael.stapelberg.ch โ€บ posts โ€บ 2026-04-05-stamp-it-all-programs-must-report-their-version
Stamp It! All Programs Must Report Their Version - Michael Stapelberg
3 weeks ago - For the gokrazy packer, which follows ... if you installed the packer from a Go module or from a git working copy. The code either displays vcs.revision (the easy case; built from git) or extracts the revision from the Go module version of the main module (BuildInfo.Main.Version): What are the other cases? These examples illustrate the scenarios I usually deal with: ... package version import ( "runtime/debug" "strings" ) func ...
๐ŸŒ
PrepBytes
prepbytes.com โ€บ home โ€บ c programming โ€บ strcpy() function in c
strcpy() Function in C
March 30, 2023 - The function copies all the characters from the source string to the destination string until it encounters a null character. The destination parameter must be large enough to hold the copied string. The strcpy function in C returns a pointer ...
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ lang โ€บ String.html
String (Java Platform SE 8 )
5 days ago - Allocates a new string that contains the sequence of characters currently contained in the string buffer argument. The contents of the string buffer are copied; subsequent modification of the string buffer does not affect the newly created string.
๐ŸŒ
Cppreference
en.cppreference.com โ€บ w โ€บ cpp โ€บ string โ€บ byte โ€บ strcpy.html
std::strcpy - cppreference.com
June 5, 2023 - The behavior is undefined if the strings overlap. ... #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'; }
Top answer
1 of 4
30

You could use strdup() to return a copy of a C-string, as in:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

stringB = strdup(stringA);
/* ... */
free(stringB);
stringB = NULL; 

You could also use strcpy(), but you need to allocate space first, which isn't hard to do but can lead to an overflow error, if not done correctly:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 ); 
strcpy( stringB, stringA );
/* ... */
free(stringB);
stringB = NULL;

If you cannot use strdup(), I would recommend the use of strncpy() instead of strcpy(). The strncpy() function copies up to โ€” and only up to โ€” n bytes, which helps avoid overflow errors. If strlen(stringA) + 1 > n, however, you would need to terminate stringB, yourself. But, generally, you'll know what sizes you need for things:

#include <string.h>

const char *stringA = "foo";
char *stringB = NULL;

/* you must add one to cover the byte needed for the terminating null character */
stringB = (char *) malloc( strlen(stringA) + 1 ); 
strncpy( stringB, stringA, strlen(stringA) + 1 );
/* ... */
free(stringB);
stringB = NULL;

I think strdup() is cleaner, myself, so I try to use it where working with strings exclusively. I don't know if there are serious downsides to the POSIX/non-POSIX approach, performance-wise, but I am not a C or C++ expert.

Note that I cast the result of malloc() to char *. This is because your question is tagged as a c++ question. In C++, it is required to cast the result from malloc(). In C, however, you would not cast this.

EDIT

There you go, there's one complication: strdup() is not in C or C++. So use strcpy() or strncp() with a pre-sized array or a malloc-ed pointer. It's a good habit to use strncp() instead of strcpy(), wherever you might use that function. It will help reduce the potential for errors.

2 of 4
4

If I just initialize stringB as char *stringB[23], because I know I'll never have a string longer than 22 characters (and allowing for the null terminator), is that the right way?

Almost. In C, if you know for sure that the string will never be too long:

char stringB[MAX+1];
assert(strlen(stringA) <= MAX));
strcpy(stringB, stringA);

or, if there's a possibility that the string might be too long:

char stringB[MAX+1];
strncpy(stringB, stringA, MAX+1);
if (stringB[MAX] != '\0') {
    // ERROR: stringA was too long.
    stringB[MAX] = '\0'; // if you want to use the truncated string
}

In C++, you should use std::string, unless you've proved that the overhead is prohibitive. Many implementations have a "short string optimisation", which will avoid dynamic allocation for short strings; in that case, there will be little or no overhead over using a C-style array. Access to individual characters is just as convenient as with a C-style array; in both cases, s[i] gives the character at position i as an lvalue. Copying becomes stringB = stringA; with no danger of undefined behaviour.

If you really do find that std::string is unusable, consider std::array<char,MAX+1>: a copyable class containing a fixed-size array.

If stringB is checked for equality with other C-strings, will the extra space affect anything?

If you use strcmp, then it will stop at the end of the shortest string, and will not be affected by the extra space.