🌐
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.
🌐
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; }
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
Copy string in C
A high-level, general-purpose ... features in addition to facilities for low-level memory manipulation. ... A C string is a nul-terminated character array. The C language does not allow assigning the contents of an array to another array. As noted by Barry, you must copy the individual ... More on learn.microsoft.com
🌐 learn.microsoft.com
3
0
September 12, 2020
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
🌐
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.
🌐
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 ...
🌐
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 C Certificate ... The <string.h> library has many functions that allow you to perform tasks on strings.
Find elsewhere
🌐
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
🌐
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 ...
🌐
IBM
ibm.com › docs › en › i › 7.4.0
strcpy() — Copy Strings
We cannot provide a description for this page right now
🌐
PerfCode
en.perfcode.com › home › c language programming › c standard library functions › c programming strcpy() function:copy string
C Programming strcpy() Function:Copy String - PerfCode
November 18, 2024 - This is a simple example of the strcpy function, which demonstrates how to copy a string from a source location to a target location. It should be noted that the target buffer must be large enough to accommodate the source string, otherwise it may cause a buffer overflow problem. #include <stdio.h> #include <string.h> int main() { char source[] = "Hello, world!"; char destination[20]; // Make sure the destination buffer is large enough strcpy(destination, source); printf("%s\n", destination); return 0; }
🌐
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.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
The first is the charAt() method: ... The other way is to treat the string as an array-like object, where individual characters correspond to a numerical index:
🌐
Electro4u
electro4u.net › blog › strcpy-function-in-c-718
Deep Dive into the strcpy() Function in C Programming: Usage and Safety
February 24, 2023 - The strcpy() function in C is used for copying strings from one character array to another. It's a fundamental string manipulation function that requires careful use to avoid buffer overflows and undefined behavior. Explore how to use the strcpy() function effectively and safely in your C programs ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › different-ways-to-copy-a-string-in-c-c
Different ways to copy a string in C/C++ - GeeksforGeeks
July 23, 2025 - We can use the inbuilt function strcpy() from <string.h> header file to copy one string to the other.
🌐
Polytechnique
lix.polytechnique.fr › ~liberti › public › computing › prog › c › C › FUNCTIONS › strcpy.html
strcpy function - LIX
main() { char *string2="red dwarf"; char *string1; string1=string2; } 'string2' is now a character pointer (only one byte) that points to a storage location containing "red dwarf" (a string constant). So string1=string2; copies the address of "red dwarf" into 'string1'.
🌐
NLRB
nlrb.gov › guidance › key-reference-materials › national-labor-relations-act
National Labor Relations Act | National Labor Relations Board
(f) [Review of final order of Board on petition to court] Any person aggrieved by a final order of the Board granting or denying in whole or in part the relief sought may obtain a review of such order in any United States court of appeals in the circuit wherein the unfair labor practice in question was alleged to have been engaged in or wherein such person resides or transacts business, or in the United States Court of Appeals for the District of Columbia, by filing in such court a written petition praying that the order of the Board be modified or set aside. A copy of such petition shall be f