I have a string to convert, string = "apple"
and want to put that into a C string of this style, char *c
, that holds {a, p, p, l, e, '\0'}
. Which predefined method should I be using?
Asked
Active
Viewed 1.5e+01k times
31

Peter Mortensen
- 30,738
- 21
- 105
- 131

teamaster
- 403
- 2
- 6
- 16
-
6`std::string` has: `string.c_str()` – pb2q Aug 06 '12 at 01:09
-
Q: How do I convert std::string to a C string? A: string.c_str() ;) – paulsm4 Aug 06 '12 at 01:11
5 Answers
43
.c_str()
returns a const char*
. If you need a mutable version, you will need to produce a copy yourself.

Yann Ramin
- 32,895
- 3
- 59
- 82
-
-
6In C++11, the functions `.data()` and `.c_str()` are synonymous, but in C++98 `.data()` returns a pointer that might not be terminated by a null character `\0`. "There are no guarantees that a null character terminates the character sequence pointed by the value returned" by `.data` – Jasha Apr 27 '16 at 05:11
9
vector<char> toVector( const std::string& s ) {
string s = "apple";
vector<char> v(s.size()+1);
memcpy( &v.front(), s.c_str(), s.size() + 1 );
return v;
}
vector<char> v = toVector(std::string("apple"));
// what you were looking for (mutable)
char* c = v.data();
.c_str() works for immutable. The vector will manage the memory for you.

bytemaster
- 572
- 2
- 5
-
How can this bad code have so many upvotes without any comment pointing out that "s" is shadowed and hence no matter what's give to the function it'll always return apple? This code was obviously not tested. – cryptearth May 09 '22 at 04:02
1
string name;
char *c_string;
getline(cin, name);
c_string = new char[name.length()];
for (int index = 0; index < name.length(); index++){
c_string[index] = name[index];
}
c_string[name.length()] = '\0';//add the null terminator at the end of
// the char array
I know this is not the predefined method but thought it may be useful to someone nevertheless.

Ritchie Shatter
- 185
- 1
- 4
- 14
-
4c_string is one byte too short if you want to add a 0 terminator like this – Johan de Vries Feb 09 '18 at 08:42
1
You can do it by 2 steps.
convert string -> const char*
const char* -> CString
string st = "my str";
const char* stBuf = st.c_str(); // 1. string to const char *
size_t sz; // save converted string's length + 1
wchar_t output[50] = L""; // return data, result is CString data
mbstowcs_s(&sz, output, 50, stBuf, 50); // converting function
CString cst = output;

Zrn-dev
- 99
- 5
0
Plain and simple:
size_t copyString2CString(char* cstr, const std::string& str, const size_t maxSize)
{
const char* data = str.c_str();
size_t copySize = str.size();
if (maxSize == 0) return maxSize; // Honestly?
if (!cstr) return 0; // Honestly???
// Mind your termination character
if (copySize + 1 > maxSize) copySize = maxSize - 1;
memcpy(cstr, data, copySize);
cstr[copySize] = '\0'; // Again: Mind your termination character
return copySize;
}
Copies content of str
to cstr
taking care of the termination character. You should provide the maximum size of cstr
via parameter maxSize
.
Do not forget to #include <cstring>
. Merry copying!

ardillapaga
- 3
- 3
-
Surely, the [`srd:strdup()` function](https://en.cppreference.com/w/c/experimental/dynamic/strdup) is even simpler: `char* cstr = std:strdup(str.c_str());`. Is not? – Adrian Mole Aug 12 '23 at 11:38
-
1Yes, it's definitely simpler! But you should be aware of the fact that the memory `cstr` is pointing to was allocated dynamically. So, give `free(cstr)` a quick call once you're done. The advantage of my definition above is that there should not be any undefined behaviour (like with `strcpy`)and that you can control the define the destination memory the source string should be copied to. – ardillapaga Aug 16 '23 at 08:29