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.

Answer from Alex Reynolds on Stack Overflow
๐ŸŒ
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; }
๐ŸŒ
Quora
quora.com โ€บ How-do-I-copy-a-string-in-C
How to copy a string in C - Quora
Answer (1 of 2): Three major ways. 1. strncpy [code]char* src = "a const string to be copied"; char dest[28] = {0}; char *strncpy(char *dest, const char *src, size_t n); dest[n]= '\0'; // terminate manually [/code] 1. strncpy copies a char array src into another char array dest up to a given ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ strncpy-function-in-c
strncpy() Function in C - GeeksforGeeks
August 5, 2024 - The strncpy() function in C is a predefined function in the string.h library used to copy a specified number of characters from one string to another. To use this function, we must include the <string.h> header file in our program.
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.

๐ŸŒ
Krivalar
krivalar.com โ€บ c-strcpy
C Programming - C and C++ - Strcpy - String Copy Example
#include<stdio.h> #include<string.h> main() { char source[]={โ€™Wโ€™,โ€˜Eโ€™,โ€˜Lโ€™,โ€˜Cโ€™, โ€˜Oโ€™,โ€˜Mโ€™,โ€˜Eโ€™}; char dest[10]=" "; printf("The source string is %s",source); strcpy(dest,source); printf("The destination string after copy %s",dest); return(0); }
Find elsewhere
๐ŸŒ
Linux Man Pages
linux.die.net โ€บ man โ€บ 3 โ€บ strcpy
strcpy(3): copy string - Linux man page
#include <string.h> char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. The strings may not overlap, and the ...
๐ŸŒ
Codingbison
codingbison.com โ€บ c โ€บ c-string-library-copying-functions.html
C String Library: Copy Functions - CodingBison
Function strcpy() copies a source string (src) into a destination string (dest), including the NUL character present at the end of the source string. The function strncpy() also does the same thing, but it only copies a maximum of "n" bytes. If strncpy() reaches NUL character (that marks the ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c strcpy()
C strcpy() - Scaler Topics
July 14, 2024 - To perform string copying operation using strcpy() function, we must add <string.h> header file using #include. ... The strcpy() is a library function available in string library in C. It is used to copy the character array pointed by the source ...
๐ŸŒ
Weber State University
icarus.cs.weber.edu โ€บ ~dab โ€บ cs1410 โ€บ textbook โ€บ 8.Strings โ€บ strcpy.html
8.2.2.2. strcpy
The source (the second argument) is copied character-by-character (including the null termination character) to the destination (the first argument). If the source is shorter than the destination, as in this example, the characters following the copied null terminator become irrelevant.
๐ŸŒ
Vultr
docs.vultr.com โ€บ clang โ€บ examples โ€บ copy-string-without-using-strcpy
C Program to Copy String Without Using strcpy() | Vultr Docs
December 4, 2024 - The expression *dst++ = *src++ not only copies the character from source to destination but also moves both pointers to the next character immediately after the assignment. This compact and efficient line performs the copy and pointer increment in a single statement. Copying a string without using strcpy() in C provides valuable insights into string handling and pointer arithmetic.
๐ŸŒ
Go Packages
pkg.go.dev โ€บ strings
strings package - strings - Go Packages
Replace returns a copy of s with all replacements performed. func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error) WriteString writes s to w with all replacements performed. ... Click to show internal directories.
๐ŸŒ
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.
๐ŸŒ
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'; }
๐ŸŒ
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 run a loop until the null character '\0' is found in the source string and copy each character from the source string to the destination string using the dereferencing operator *. Example: Program to copy the string using pointers
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ safest way to copy a string?
r/C_Programming on Reddit: Safest way to copy a string?
May 7, 2023 -

I just fell foul of the fact that strncpy does not add an old terminator if the destination buffer is shorter than the source string. Is there a single function standard library replacement that I could drop in to the various places strncpy is used that would copy a null terminated string up to the length of the destination buffer, guaranteeing early (but correct) termination of the destination string, if the destination buffer is too short?

Edit:

  • Yes, I do need C-null terminated strings. This C API is called by something else that provides a buffer for me to copy into, with the expectation that itโ€™s null terminated

Edit 2:

  • I know I can write a helper function thatโ€™s shared across relevant parts of the code, but I donโ€™t want to do that because then each of those modules that need the function becomes coupled to a shared helper header file, which is fine in isolation but โ€œoh I want to use this code in another project, better make sure I take all the misc dependenciesโ€ is best avoided. Necessary if necessary, but if possible using a standard function, even better.

๐ŸŒ
Antonz
antonz.org โ€บ porting-go-strings
Porting Go's strings package to C
1 month ago - I started with the io package, which provides core abstractions like Reader and Writer, as well as general-purpose functions like Copy. But io isn't very interesting on its own, since it doesn't include specific reader or writer implementations. So my next choices were naturally bytes and strings ...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2017 โ€บ 11 โ€บ c-strcpy-function
C strcpy() Function โ€“ C tutorial
#include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; //copying the string "Apple" to the str1 strcpy(str1, "Apple"); printf("String str1: %s\n", str1); //copying the string "Banana" to the str2 strcpy(str2, "Banana"); printf("String str2: %s\n", str2); //copying the value of str2 to the string str1 strcpy(str1, str2); printf("String str1: %s\n", str1); return 0; }