How can I convert an std::string to a char* or a const char*?
If you just want to pass a std::string to a function that needs const char *, you can use .c_str():
std::string str;
const char * c = str.c_str();
And if you need a non-const char *, call .data():
std::string str;
char * c = str.data();
.data() was added in C++17. Before that, you can use &str[0].
Note that if the std::string is const, .data() will return const char * instead, like .c_str().
The pointer becomes invalid if the string is destroyed or reallocates memory.
The pointer points to a null-terminated string, and the terminator doesn't count against str.size(). You're not allowed to assign a non-null character to the terminator.
Given say...
std::string x = "hello";
Getting a `char *` or `const char*` from a `string`
How to get a character pointer that's valid while x remains in scope and isn't modified further
C++11 simplifies things; the following all give access to the same internal string buffer:
const char* p_c_str = x.c_str();
const char* p_data = x.data();
char* p_writable_data = x.data(); // for non-const x from C++17
const char* p_x0 = &x[0];
char* p_x0_rw = &x[0]; // compiles iff x is not const...
All the above pointers will hold the same value - the address of the first character in the buffer. Even an empty string has a "first character in the buffer", because C++11 guarantees to always keep an extra NUL/0 terminator character after the explicitly assigned string content (e.g. std::string("this\0that", 9) will have a buffer holding "this\0that\0").
Given any of the above pointers:
char c = p[n]; // valid for n <= x.size()
// i.e. you can safely read the NUL at p[x.size()]
Only for the non-const pointer p_writable_data and from &x[0]:
p_writable_data[n] = c;
p_x0_rw[n] = c; // valid for n <= x.size() - 1
// i.e. don't overwrite the implementation maintained NUL
Writing a NUL elsewhere in the string does not change the string's size(); string's are allowed to contain any number of NULs - they are given no special treatment by std::string (same in C++03).
In C++03, things were considerably more complicated (key differences highlighted):
x.data()
returns const char* to the string's internal buffer which wasn't required by the Standard to conclude with a NUL (i.e. might be ['h', 'e', 'l', 'l', 'o'] followed by uninitialised or garbage values, with accidental accesses thereto having undefined behaviour).
x.size() characters are safe to read, i.e. x[0] through x[x.size() - 1]
for empty strings, you're guaranteed some non-NULL pointer to which 0 can be safely added (hurray!), but you shouldn't dereference that pointer.
&x[0]
for empty strings this has undefined behaviour (21.3.4)
e.g. given f(const char* p, size_t n) { if (n == 0) return; ...whatever... } you mustn't call f(&x[0], x.size()); when x.empty() - just use f(x.data(), ...).
otherwise, as per x.data() but:
for non-const x this yields a non-const char* pointer; you can overwrite string content
x.c_str()
returns const char* to an ASCIIZ (NUL-terminated) representation of the value (i.e. ['h', 'e', 'l', 'l', 'o', '\0']).
although few if any implementations chose to do so, the C++03 Standard was worded to allow the string implementation the freedom to create a distinct NUL-terminated buffer on the fly, from the potentially non-NUL terminated buffer "exposed" by x.data() and &x[0]
x.size() + 1 characters are safe to read.
guaranteed safe even for empty strings (['\0']).
Consequences of accessing outside legal indices
Whichever way you get a pointer, you must not access memory further along from the pointer than the characters guaranteed present in the descriptions above. Attempts to do so have undefined behaviour, with a very real chance of application crashes and garbage results even for reads, and additionally wholesale data, stack corruption and/or security vulnerabilities for writes.
When do those pointers get invalidated?
If you call some string member function that modifies the string or reserves further capacity, any pointer values returned beforehand by any of the above methods are invalidated. You can use those methods again to get another pointer. (The rules are the same as for iterators into strings).
See also How to get a character pointer valid even after x leaves scope or is modified further below....
So, which is better to use?
From C++11, use .c_str() for ASCIIZ data, and .data() for "binary" data (explained further below).
In C++03, use .c_str() unless certain that .data() is adequate, and prefer .data() over &x[0] as it's safe for empty strings....
...try to understand the program enough to use data() when appropriate, or you'll probably make other mistakes...
The ASCII NUL '\0' character guaranteed by .c_str() is used by many functions as a sentinel value denoting the end of relevant and safe-to-access data. This applies to both C++-only functions like say fstream::fstream(const char* filename, ...) and shared-with-C functions like strchr(), and printf().
Given C++03's .c_str()'s guarantees about the returned buffer are a super-set of .data()'s, you can always safely use .c_str(), but people sometimes don't because:
using .data() communicates to other programmers reading the source code that the data is not ASCIIZ (rather, you're using the string to store a block of data (which sometimes isn't even really textual)), or that you're passing it to another function that treats it as a block of "binary" data. This can be a crucial insight in ensuring that other programmers' code changes continue to handle the data properly.
C++03 only: there's a slight chance that your string implementation will need to do some extra memory allocation and/or data copying in order to prepare the NUL terminated buffer
As a further hint, if a function's parameters require the (const) char* but don't insist on getting x.size(), the function probably needs an ASCIIZ input, so .c_str() is a good choice (the function needs to know where the text terminates somehow, so if it's not a separate parameter it can only be a convention like a length-prefix or sentinel or some fixed expected length).
How to get a character pointer valid even after x leaves scope or is modified further
You'll need to copy the contents of the string x to a new memory area outside x. This external buffer could be in many places such as another string or character array variable, it may or may not have a different lifetime than x due to being in a different scope (e.g. namespace, global, static, heap, shared memory, memory mapped file).
To copy the text from std::string x into an independent character array:
// USING ANOTHER STRING - AUTO MEMORY MANAGEMENT, EXCEPTION SAFE
std::string old_x = x;
// - old_x will not be affected by subsequent modifications to x...
// - you can use `&old_x[0]` to get a writable char* to old_x's textual content
// - you can use resize() to reduce/expand the string
// - resizing isn't possible from within a function passed only the char* address
std::string old_x = x.c_str(); // old_x will terminate early if x embeds NUL
// Copies ASCIIZ data but could be less efficient as it needs to scan memory to
// find the NUL terminator indicating string length before allocating that amount
// of memory to copy into, or more efficient if it ends up allocating/copying a
// lot less content.
// Example, x == "ab\0cd" -> old_x == "ab".
// USING A VECTOR OF CHAR - AUTO, EXCEPTION SAFE, HINTS AT BINARY CONTENT, GUARANTEED CONTIGUOUS EVEN IN C++03
std::vector<char> old_x(x.data(), x.data() + x.size()); // without the NUL
std::vector<char> old_x(x.c_str(), x.c_str() + x.size() + 1); // with the NUL
// USING STACK WHERE MAXIMUM SIZE OF x IS KNOWN TO BE COMPILE-TIME CONSTANT "N"
// (a bit dangerous, as "known" things are sometimes wrong and often become wrong)
char y[N + 1];
strcpy(y, x.c_str());
// USING STACK WHERE UNEXPECTEDLY LONG x IS TRUNCATED (e.g. Hello\0->Hel\0)
char y[N + 1];
strncpy(y, x.c_str(), N); // copy at most N, zero-padding if shorter
y[N] = '\0'; // ensure NUL terminated
// USING THE STACK TO HANDLE x OF UNKNOWN (BUT SANE) LENGTH
char* y = alloca(x.size() + 1);
strcpy(y, x.c_str());
// USING THE STACK TO HANDLE x OF UNKNOWN LENGTH (NON-STANDARD GCC EXTENSION)
char y[x.size() + 1];
strcpy(y, x.c_str());
// USING new/delete HEAP MEMORY, MANUAL DEALLOC, NO INHERENT EXCEPTION SAFETY
char* y = new char[x.size() + 1];
strcpy(y, x.c_str());
// or as a one-liner: char* y = strcpy(new char[x.size() + 1], x.c_str());
// use y...
delete[] y; // make sure no break, return, throw or branching bypasses this
// USING new/delete HEAP MEMORY, SMART POINTER DEALLOCATION, EXCEPTION SAFE
// see boost shared_array usage in Johannes Schaub's answer
// USING malloc/free HEAP MEMORY, MANUAL DEALLOC, NO INHERENT EXCEPTION SAFETY
char* y = strdup(x.c_str());
// use y...
free(y);
Other reasons to want a char* or const char* generated from a string
So, above you've seen how to get a (const) char*, and how to make a copy of the text independent of the original string, but what can you do with it? A random smattering of examples...
give "C" code access to the C++ string's text, as in printf("x is '%s'", x.c_str());
copy x's text to a buffer specified by your function's caller (e.g. strncpy(callers_buffer, callers_buffer_size, x.c_str())), or volatile memory used for device I/O (e.g. for (const char* p = x.c_str(); *p; ++p) *p_device = *p;)
append x's text to an character array already containing some ASCIIZ text (e.g. strcat(other_buffer, x.c_str())) - be careful not to overrun the buffer (in many situations you may need to use strncat)
return a const char* or char* from a function (perhaps for historical reasons - client's using your existing API - or for C compatibility you don't want to return a std::string, but do want to copy your string's data somewhere for the caller)
be careful not to return a pointer that may be dereferenced by the caller after a local string variable to which that pointer pointed has left scope
some projects with shared objects compiled/linked for different std::string implementations (e.g. STLport and compiler-native) may pass data as ASCIIZ to avoid conflicts
Use the .c_str() method for const char *.
You can use &mystring[0] to get a char * pointer, but there are a couple of gotcha's: you won't necessarily get a zero terminated string, and you won't be able to change the string's size. You especially have to be careful not to add characters past the end of the string or you'll get a buffer overrun (and probable crash).
There was no guarantee that all of the characters would be part of the same contiguous buffer until C++11, but in practice all known implementations of std::string worked that way anyway; see Does “&s[0]” point to contiguous characters in a std::string?.
Note that many string member functions will reallocate the internal buffer and invalidate any pointers you might have saved. Best to use them immediately and then discard.
C++17
C++17 (upcoming standard) changes the synopsis of the template basic_string adding a non const overload of data():
charT* data() noexcept;
Returns: A pointer p such that p + i == &operator for each i in [0,size()].
CharT const * from std::basic_string<CharT>
std::string const cstr = { "..." };
char const * p = cstr.data(); // or .c_str()
CharT * from std::basic_string<CharT>
std::string str = { "..." };
char * p = str.data();
C++11
CharT const * from std::basic_string<CharT>
std::string str = { "..." };
str.c_str();
CharT * from std::basic_string<CharT>
From C++11 onwards, the standard says:
The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().
const_reference operator[](size_type pos) const; reference operator[](size_type pos);
Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object of type CharT with value CharT(); the referenced value shall not be modified.
const charT* c_str() const noexcept;const charT* data() const noexcept;
Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
There are severable possible ways to get a non const character pointer.
1. Use the contiguous storage of C++11
std::string foo{"text"};
auto p = &*foo.begin();
Pro
Simple and short
Fast (only method with no copy involved)
Cons
Final '\0' is not to be altered / not necessarily part of the non-const memory.
2. Use std::vector<CharT>
std::string foo{"text"};
std::vector<char> fcv(foo.data(), foo.data()+foo.size()+1u);
auto p = fcv.data();
Pro
Simple
Automatic memory handling
Dynamic
Cons
Requires string copy
3. Use std::array<CharT, N> if N is compile time constant (and small enough)
std::string foo{"text"};
std::array<char, 5u> fca;
std::copy(foo.data(), foo.data()+foo.size()+1u, fca.begin());
Pro
Simple
Stack memory handling
Cons
Static
Requires string copy
4. Raw memory allocation with automatic storage deletion
std::string foo{ "text" };
auto p = std::make_unique<char[]>(foo.size()+1u);
std::copy(foo.data(), foo.data() + foo.size() + 1u, &p[0]);
Pro
Small memory footprint
Automatic deletion
Simple
Cons
Requires string copy
Static (dynamic usage requires lots more code)
Less features than vector or array
5. Raw memory allocation with manual handling
std::string foo{ "text" };
char * p = nullptr;
try
{
p = new char[foo.size() + 1u];
std::copy(foo.data(), foo.data() + foo.size() + 1u, p);
// handle stuff with p
delete[] p;
}
catch (...)
{
if (p) { delete[] p; }
throw;
}
Pro
Maximum 'control'
Con
Requires string copy
Maximum liability / susceptibility for errors
Complex
Just see this:
string str1("stackoverflow");
const char * str2 = str1.c_str();
However, note that this will return a const char *.
For a char *, use strcpy to copy it into another char array.
I am working with an API with a lot of functions that get a char* as an input.
I have created a small class to face this kind of problem, and I have implemented the RAII idiom.
class DeepString
{
DeepString(const DeepString& other);
DeepString& operator=(const DeepString& other);
char* internal_;
public:
explicit DeepString( const string& toCopy):
internal_(new char[toCopy.size()+1])
{
strcpy(internal_,toCopy.c_str());
}
~DeepString() { delete[] internal_; }
char* str() const { return internal_; }
const char* c_str() const { return internal_; }
};
And you can use it as:
void aFunctionAPI(char* input);
// other stuff
aFunctionAPI("Foo"); //this call is not safe. if the function modified the
//literal string the program will crash
std::string myFoo("Foo");
aFunctionAPI(myFoo.c_str()); //this is not compiling
aFunctionAPI(const_cast<char*>(myFoo.c_str())); //this is not safe std::string
//implement reference counting and
//it may change the value of other
//strings as well.
DeepString myDeepFoo(myFoo);
aFunctionAPI(myFoo.str()); //this is fine
I have called the class DeepString because it is creating a deep and unique copy (the DeepString is not copyable) of an existing string.
char* result = strcpy((char*)malloc(str.length()+1), str.c_str());
Converting from c++ std string to C style string is really easy now.
For that we have string::copy function which will easily convert std string to C style string. reference
string::copy functions parameters serially
char string pointer
string size, how many characters will b copied
position, from where character copy will start
Another important thing,
This function does not append a null character at the end of operation. So, we need to put it manually.
Code exam are in below -
// char string
char chText[20];
// c++ string
string text = "I am a Programmer";
// conversion from c++ string to char string
// this function does not append a null character at the end of operation
text.copy(chText, text.size(), 0);
// we need to put it manually
chText[text.size()] = '\0';
// below statement prints "I am a Programmer"
cout << chText << endl;
Vice Versa, Converting from C style string to C++ std string is lot more easier
There is three ways we can convert from C style string to C++ std string
First one is using constructor,
char chText[20] = "I am a Programmer";
// using constructor
string text(chText);
Second one is using string::assign method
// char string
char chText[20] = "I am a Programmer";
// c++ string
string text;
// convertion from char string to c++ string
// using assign function
text.assign(chText);
Third one is assignment operator(=), in which string class uses operator overloading
// char string
char chText[20] = "I am a Programmer";
// c++ string
// convertion from char string to c++ string using assignment operator overloading
string text = chText;
third one can be also write like below -
// char string
char chText[20] = "I am a Programmer";
// c++ string
string text;
// convertion from char string to c++ string
text = chText;
let's say,
string str="stack";
1)converting string to char*
char* s_rw=&str[0];
The above char*(i.e., s_rw) is readable and writeable and points to the base
address of the string which needs to be converted to char*
2)Converting string to const char*
const char* s_r=&str[0];
The above const char* (i.e., s_r) is readable but not writeable and points to the
base address of the string.
This is especially useful when passing the underlying char* buffer of a std::string to C calls which expect and write to a char* buffer. This way you get the best of both worlds!: the niceties of the C++ std::string and the usability of it directly with C libraries you are calling from C++.
How to use a modern C++ std::string as a C-style read/writable char* or read-only null-terminated const char*
How can I convert a std::string to a char* or a const char*?
Despite being a really old and highly-upvoted question, the information I'm about to cover isn't already well-covered, if covered at all, so this is a necessary addition, in particular the part about needing to pre-allocate the underlying C-string using the .resize() method if you'd like to use it as a writable buffer.
All of the usages below require C++11 or later, except for the char* data() call, which requires C++17 or later.
To run and test all example code below, and more, see and run my string__use_std_string_as_a_c_str_buffer.cpp file in my eRCaGuy_hello_world repo.
Quick summary:
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
// IMPORTANT: pre-allocate the underlying buffer to guarantee what size it is
str.resize(BUFFER_SIZE);
// -----------------------------------------------------------------------------
// Get read-writeable access to the underlying `char*` C-string at index i
// -----------------------------------------------------------------------------
char* c_str1 = &str[i]; // <=== my favorite!
char* c_str2 = str.data() + i;
char* c_str3 = &(*str.begin()) + i;
// NB: the C-strings above are NOT guaranteed to be null-terminated, so manually
// write in a null terminator at the index location where you want it if
// desired. Ex:
//
// 1. write a null terminator at some arbitrary position you choose (index 10
// here)
c_str1[10] = '\0';
// 2. write a null terminator at the last guaranteed valid position in the
// underlying C-string/array of chars
c_str2[str.size() - i - 1] = '\0';
// -----------------------------------------------------------------------------
// Get read-only access to the underlying `const char*` C-string at index i
// -----------------------------------------------------------------------------
const char* const_c_str1 = &str[i];
const char* const_c_str2 = str.c_str() + i; // guaranteed to be null-terminated,
// but not necessarily at the
// position you desire; the
// guaranteed null terminator will
// be at index location
// `str.size()`
Summary:
If in a hurry and you need:
A read-writable char* C-string of the underlying buffer: just use section A Technique 1 in the code example just below: char* c_str1 = &str[i];.
Just be sure to pre-allocate the underlying buffer size first via str.resize(BUFFER_SIZE), if needed, is all, to ensure the underlying buffer is big enough for your needs.
A read-only const char* C-string of the underlying buffer: use the same thing as above (const char* const_c_str1 = &str[i];), or const char* const_c_str1 = str.c_str() + i;.
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
// IMPORTANT: pre-allocate the underlying buffer to guarantee what size it is
str.resize(BUFFER_SIZE);
// =============================================================================
// Now you can use the `std::string`'s underlying buffer directly as a C-string
// =============================================================================
// ---------------------------------------------------------
// A. As a read-writeable `char*` C-string
// ---------------------------------------------------------
// Technique 1 [best option if using C++11]: array indexing using `operator[]`
// to obtain a char, followed by obtaining its address with `&`
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/operator_at
char* c_str1 = &str[0];
char* c_str2 = &str[10];
char* c_str3 = &str[33];
// etc.
// Technique 2 [best option if using C++17]: use the `.data()` method to obtain
// a `char*` directly.
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/data
char* c_str11 = str.data(); // same as c_str1 above
char* c_str12 = str.data() + 10; // same as c_str2 above
char* c_str13 = str.data() + 33; // same as c_str3 above
// Technique 3 [fine in C++11 or later, but is awkward, so don't do this. It is
// for demonstration and learning purposes only]: use the `.begin()` method to
// obtain an iterator to the first char, and then use the iterator's
// `operator*()` dereference method to obtain the iterator's `char`
// `value_type`, and then take the address of that to obtain a `char*`
// - Documentation:
// - https://en.cppreference.com/w/cpp/string/basic_string/begin
// - https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
char* c_str21 = &(*str.begin()); // same as c_str1 and c_str11 above
char* c_str22 = &(*str.begin()) + 10; // same as c_str2 and c_str12 above
char* c_str23 = &(*str.begin()) + 33; // same as c_str3 and c_str13 above
// ---------------------------------------------------------
// B. As a read-only, null-terminated `const char*` C-string
// ---------------------------------------------------------
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/c_str
const char* const_c_str1 = str.c_str(); // a const version of c_str1 above
const char* const_c_str2 = str.c_str() + 10; // a const version of c_str2 above
const char* const_c_str3 = str.c_str() + 33; // a const version of c_str3 above
Note that you can also use the .at(i) and .front() std::string methods too, but I won't go into those since I think my examples are sufficient. For their documentation, see:
https://en.cppreference.com/w/cpp/string/basic_string/at
https://en.cppreference.com/w/cpp/string/basic_string/front
Details:
See also the note just above. I'm not going to cover the techniques using the .at(i) and .front() std::string methods, since I think the several techniques I already present are sufficient.
1. Use a std::string as a read/writable char*
To use a C++ std::string as a C-style writable char* buffer, you MUST first pre-allocate the string's internal buffer to change its .size() by using .resize(). Note that using .reserve() to increase only the .capacity() is NOT sufficient! The cppreference.com community wiki page for std::string::operator[] correctly states:
If pos > size(), the behavior is undefined.
The resize() method is what changes the size, not the reserve() method, which changes only the capacity().
Ex:
#include <cstring> // `strcpy()`
#include <iostream>
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
str.resize(BUFFER_SIZE); // pre-allocate the underlying buffer
// check the size
std::cout << "str.size() = " << str.size() << "\n";
For all examples below, assume you have these C-strings:
constexpr char cstr1[] = "abcde ";
constexpr char cstr2[] = "fghijk";
Once you have pre-allocated an underlying buffer which is sufficiently large with resize(), you can then access the underlying buffer as
a char* in at least 3 ways:
Technique 1 [best option if using C++11]: array indexing using operator[] to obtain a char, followed by obtaining its address with &. Ex:
char* c_str;
c_str = &str[0];
c_str = &str[5];
// etc.
// Write these 2 C-strings into a `std::string`'s underlying buffer
strcpy(&str[0], cstr1);
strcpy(&str[sizeof(cstr1) - 1], cstr2); // `- 1` to overwrite the first
// null terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
What if you have a pointer to a std::string? If you have a ptr to a std::string, it must be dereferenced first with *pstr before you can index into it as an array with the operator[] as &(*pstr)[0], so the syntax above becomes a little more awkward. Here is a full example:
std::string str2;
std::string* pstr = &str2;
pstr->resize(BUFFER_SIZE);
c_str = &(*pstr)[0]; // <=== dereference the ptr 1st before indexing into it
// Or, to make the order of precedence
// (https://en.cppreference.com/w/cpp/language/operator_precedence) really
// obvious, you can optionally add extra parenthesis like this:
c_str = &((*pstr)[0]);
Technique 2 [best option if using C++17]: use the .data() method to obtain a char* directly. Ex:
char* c_str;
c_str = str.data();
c_str = str.data() + 5;
// etc.
// Write these 2 C-strings into the `std::string`'s underlying buffer
strcpy(str.data(), cstr1);
strcpy(str.data() + (sizeof(cstr1) - 1), cstr2); // `- 1` to overwrite the
// first null terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
Technique 3 [fine in C++11 and later, but is awkward, so don't do this. It is for demonstration and learning purposes only]: use the .begin() method to obtain an iterator to the first char, and then use the iterator's operator*() dereference method to obtain the iterator's char value_type, and then take the address of that to obtain a char*. Ex:
char* c_str;
c_str = &(*str.begin());
c_str = &(*str.begin()) + 5;
// etc.
// Write these 2 C-strings into the `std::string`'s underlying buffer
strcpy(&(*str.begin()), cstr1);
strcpy(&(*str.begin()) + (sizeof(cstr1) - 1), cstr2); // `- 1` to overwrite
// the first null
// terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
Something important to be aware of is that when you call str.resize(100), it reserves at least 100 bytes for the underlying string, sets the size() of the string to 100, and initializes all 100 of those chars to char()--AKA: the default value initialization value for char (see my question here), which is the binary zero null-terminator, '\0'. Therefore, whenever you call str.size() it will return 100 even if the string simply has "hello" in it followed by 95 null-terminators, or zeros. To get the length, or number of non-null-terminators in the string, you'll have to resort to the C function strlen(), like this:
std::cout << strlen(str.c_str()) << "\n"; // prints `12` in the examples above
// instead of:
std::cout << str.size() << "\n"; // prints `100` in the examples above
2. Access a std::string as a read-only, null-terminated const char*
To obtain a readable null-terminated const char* from a std::string, use the .c_str() method. It returns a C-style string that is guaranteed to be null-terminated. Note that the .data() method is NOT the same thing, as it is NOT guaranteed to be null-terminated!
Example:
std::string str = "hello world";
printf("%s\n", str.c_str());
References
(questions on Stack Overflow)
How to convert a std::string to const char* or char*:
How to convert a std::string to const char* or char*
Directly write into char* buffer of std::string:
Directly write into char* buffer of std::string
Is there a way to get std:string's buffer:
Is there a way to get std:string's buffer
(my content)
[my test code] string__use_std_string_as_a_c_str_buffer.cpp
[my Q] See the "Adjacently related" section at the bottom of my question here:
What is a call to `char()`, `uint8_t()`, `int64_t()`, integer `T()`, etc, as a function in C++?
*****+ [my comments about pre-allocating a buffer in the std::string]:
Directly write into char* buffer of std::string
*****+ [my comment on how to pre-allocate storage in a std::string, to be used as a char* buffer]
Is there a way to get std:string's buffer
(from the cppreference.com community wiki)
https://en.cppreference.com/w/cpp/string/basic_string:
The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin
() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s[0] can
be passed to functions that expect a pointer to the first element of a null-terminated
(since C++11)CharT[] array.
https://en.cppreference.com/w/cpp/string/basic_string/operator_at
Returns a reference to the character at specified location pos. No bounds checking is performed.
If pos > size(), the behavior is undefined.
https://en.cppreference.com/w/cpp/string/basic_string/resize
https://en.cppreference.com/w/cpp/string/basic_string/reserve
https://en.cppreference.com/w/cpp/string/basic_string/data
https://en.cppreference.com/w/cpp/string/basic_string/c_str
https://en.cppreference.com/w/cpp/string/basic_string/clear
Try this
std::string s(reinterpret_cast<const char *>(Data), Size);
Related
I want to know more about programming and after a bit of googling I found how to convert a string to a const char.
String text1;
What I do not understand is why c_str() works,
const char *text2 = text1.c_str();
contrary to toCharArray()?
const char *text2 = text1.toCharArray();
or
const char text2 = text1.toCharArray();
The latter is more logical to me as I want to convert a string to a char, and then turn it into a const char. But that doesn't work because one is a string, the other is a char. The former, as I understand, converts the string to a C-type string and then turns it into a const char. Here, the string suddenly isn't an issue anymore oO
.
a) Why does it need a C-type string conversion and why does it work only then?
b) Why is the pointer needed?
c) Why does a simple toCharArray() not work?
.
Or do I do something terribly wrong?
Thanks heaps.
I am using PlatformIO with Arduino platform.
If you need to modify the returned c-style string in any way, or have it persist after you modify the original String, you should use toCharArray.
If you only need a null-terminated c-style string to pass as a read-only parameter to a function, use c_str.
Arduino reference for String.toCharArray()
Arduino reference for String.c_str()
The interface (and implementation) of toCharArray is shown below, from source
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
{ getBytes((unsigned char *)buf, bufsize, index); }
So your first issue is that you're trying to use it incorrectly. toCharArray will COPY the underlying characters of your String into a buffer that you provide. This must be extra space that you have allocated, either in a buffer on the stack, or in some other writable area of memory. You would do it like this.
String str = "I am a string!";
char buf[5];
str.toCharArray(buf, 5);
// buf is now "I am\0"
// or you can start at a later index, here index 5
str.toCharArray(buf, 5, 5);
// buf is now "a st\0"
// we can also change characters in the buffer
buf[1] = 'X';
// buf is now "aXst\0"
// modifying the original String does not invalidate the buffer
str = "Je suis une chaine!";
// buf is still "aXst\0"
This allows you to copy a string partially, or at a later index, or anything you want. Most importantly, this array you copy into is mutable. We can change it, and since it's a copy, it doesn't affect the original String we copied it from. This flexibility comes with a cost. First, we have to have a large enough buffer, which may not be known at compile time, and takes up memory. Second, that copying takes time to do.
But what if we're calling a function that just wants to read a c-style string as input? It doesn't need to modify it at all?
That's where c_str() comes in. The String object has an underlying c-string type array (yes, null terminator and all). c_str() simply returns a const char* to this array. We make it const so that we don't accidentally change it. An object's underlying data should not be changed by random functions outside of its control.
This is the ENTIRE code for c_str():
const char* c_str() const { return buffer; }
You already know how to use it, but to illustrate a difference:
String str = "I am another string!";
const char* c = str.c_str();
// c[1] = 'X'; // error, cannot modify a const object
// modifying the original string may reallocate the underlying buffer
str = "Je suis une autre chaine!";
// dereferencing c now may point to invalid memory
Since c_str() simply returns the underlying data pointer, it's fast. But we don't want other functions to be allowed to modify this data, so it's const.
I wanted to access character pointer ith element. Below is the sample code
string a_value = "abcd";
char *char_p=const_cast<char *>(a_value.c_str());
if(char_p[2] == 'b') //Is this safe to use across all platform?
{
//do soemthing
}
Thanks in advance
Array accessors [] are allowed for pointer types, and result in defined and predictable behaviors if the offset inside [] refers to valid memory.
const char* ptr = str.c_str();
if (ptr[2] == '2') {
...
}
Is correct on all platforms if the length of str is 3 characters or more.
In general, if you are not mutating the char* you are looking at, it best to avoid a const_cast and work with a const char*. Also note that std::string provides operator[] which means that you do not need to call .c_str() on str to be able to index into it and look at a char. This will similarly be correct on all platforms if the length of str is 3 characters or more. If you do not know the length of the string in advance, use std::string::at(size_t pos), which performs bound checking and throws an out_of_range exception if the check fails.
You can access the ith element in a std::string using its operator[]() like this:
std::string a_value = "abcd";
if (a_value[2] == 'b')
{
// do stuff
}
If you use a C++11 conformant std::string implementation you can also use:
std::string a_value = "abcd";
char const * p = &a_value[0];
// or char const * p = a_value.data();
// or char const * p = a_value.c_str();
// or char * p = &a_value[0];
21.4.1/5
The char-like objects in a basic_string object shall be stored contiguously.
21.4.7.1/1: c_str() / data()
Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
The question is essentially about querying characters in a string safely.
const char* a = a_value.c_str();
is safe unless some other operation modifies the string after it. If you can guarantee that no other code performs a modification prior to using a, then you have safely retrieved a pointer to a null-terminated string of characters.
char* a = const_cast<char *>(a_value.c_str());
is never safe. You have yielded a pointer to memory that is writeable. However, that memory was never designed to be written to. There is no guarantee that writing to that memory will actually modify the string (and actually no guarantee that it won't cause a core dump). It's undefined behaviour - absolutely unsafe.
reference here: http://en.cppreference.com/w/cpp/string/basic_string/c_str
addressing a[2] is safe provided you can prove that all possible code paths ensure that a represents a pointer to memory longer than 2 chars.
If you want safety, use either:
auto ch = a_string.at(2); // will throw an exception if a_string is too short.
or
if (a_string.length() > 2) {
auto ch = a_string[2];
}
else {
// do something else
}
Everyone explained very well for most how it's safe, but i'd like to extend a bit if that's ok.
Since you're in C++, and you're using a string, you can simply do the following to access a caracter (and you won't have any trouble, and you still won't have to deal with cstrings in cpp :
std::string a_value = "abcd";
std::cout << a_value.at(2);
Which is in my opinion a better option rather than going out of the way.
string::at will return a char & or a const char& depending on your string object. (In this case, a const char &)
In this case you can treat char* as an array of chars (C-string). Parenthesis is allowed.
How can I convert an std::string to a char* or a const char*?
If you just want to pass a std::string to a function that needs const char *, you can use .c_str():
std::string str;
const char * c = str.c_str();
And if you need a non-const char *, call .data():
std::string str;
char * c = str.data();
.data() was added in C++17. Before that, you can use &str[0].
Note that if the std::string is const, .data() will return const char * instead, like .c_str().
The pointer becomes invalid if the string is destroyed or reallocates memory.
The pointer points to a null-terminated string, and the terminator doesn't count against str.size(). You're not allowed to assign a non-null character to the terminator.
Given say...
std::string x = "hello";
Getting a `char *` or `const char*` from a `string`
How to get a character pointer that's valid while x remains in scope and isn't modified further
C++11 simplifies things; the following all give access to the same internal string buffer:
const char* p_c_str = x.c_str();
const char* p_data = x.data();
char* p_writable_data = x.data(); // for non-const x from C++17
const char* p_x0 = &x[0];
char* p_x0_rw = &x[0]; // compiles iff x is not const...
All the above pointers will hold the same value - the address of the first character in the buffer. Even an empty string has a "first character in the buffer", because C++11 guarantees to always keep an extra NUL/0 terminator character after the explicitly assigned string content (e.g. std::string("this\0that", 9) will have a buffer holding "this\0that\0").
Given any of the above pointers:
char c = p[n]; // valid for n <= x.size()
// i.e. you can safely read the NUL at p[x.size()]
Only for the non-const pointer p_writable_data and from &x[0]:
p_writable_data[n] = c;
p_x0_rw[n] = c; // valid for n <= x.size() - 1
// i.e. don't overwrite the implementation maintained NUL
Writing a NUL elsewhere in the string does not change the string's size(); string's are allowed to contain any number of NULs - they are given no special treatment by std::string (same in C++03).
In C++03, things were considerably more complicated (key differences highlighted):
x.data()
returns const char* to the string's internal buffer which wasn't required by the Standard to conclude with a NUL (i.e. might be ['h', 'e', 'l', 'l', 'o'] followed by uninitialised or garbage values, with accidental accesses thereto having undefined behaviour).
x.size() characters are safe to read, i.e. x[0] through x[x.size() - 1]
for empty strings, you're guaranteed some non-NULL pointer to which 0 can be safely added (hurray!), but you shouldn't dereference that pointer.
&x[0]
for empty strings this has undefined behaviour (21.3.4)
e.g. given f(const char* p, size_t n) { if (n == 0) return; ...whatever... } you mustn't call f(&x[0], x.size()); when x.empty() - just use f(x.data(), ...).
otherwise, as per x.data() but:
for non-const x this yields a non-const char* pointer; you can overwrite string content
x.c_str()
returns const char* to an ASCIIZ (NUL-terminated) representation of the value (i.e. ['h', 'e', 'l', 'l', 'o', '\0']).
although few if any implementations chose to do so, the C++03 Standard was worded to allow the string implementation the freedom to create a distinct NUL-terminated buffer on the fly, from the potentially non-NUL terminated buffer "exposed" by x.data() and &x[0]
x.size() + 1 characters are safe to read.
guaranteed safe even for empty strings (['\0']).
Consequences of accessing outside legal indices
Whichever way you get a pointer, you must not access memory further along from the pointer than the characters guaranteed present in the descriptions above. Attempts to do so have undefined behaviour, with a very real chance of application crashes and garbage results even for reads, and additionally wholesale data, stack corruption and/or security vulnerabilities for writes.
When do those pointers get invalidated?
If you call some string member function that modifies the string or reserves further capacity, any pointer values returned beforehand by any of the above methods are invalidated. You can use those methods again to get another pointer. (The rules are the same as for iterators into strings).
See also How to get a character pointer valid even after x leaves scope or is modified further below....
So, which is better to use?
From C++11, use .c_str() for ASCIIZ data, and .data() for "binary" data (explained further below).
In C++03, use .c_str() unless certain that .data() is adequate, and prefer .data() over &x[0] as it's safe for empty strings....
...try to understand the program enough to use data() when appropriate, or you'll probably make other mistakes...
The ASCII NUL '\0' character guaranteed by .c_str() is used by many functions as a sentinel value denoting the end of relevant and safe-to-access data. This applies to both C++-only functions like say fstream::fstream(const char* filename, ...) and shared-with-C functions like strchr(), and printf().
Given C++03's .c_str()'s guarantees about the returned buffer are a super-set of .data()'s, you can always safely use .c_str(), but people sometimes don't because:
using .data() communicates to other programmers reading the source code that the data is not ASCIIZ (rather, you're using the string to store a block of data (which sometimes isn't even really textual)), or that you're passing it to another function that treats it as a block of "binary" data. This can be a crucial insight in ensuring that other programmers' code changes continue to handle the data properly.
C++03 only: there's a slight chance that your string implementation will need to do some extra memory allocation and/or data copying in order to prepare the NUL terminated buffer
As a further hint, if a function's parameters require the (const) char* but don't insist on getting x.size(), the function probably needs an ASCIIZ input, so .c_str() is a good choice (the function needs to know where the text terminates somehow, so if it's not a separate parameter it can only be a convention like a length-prefix or sentinel or some fixed expected length).
How to get a character pointer valid even after x leaves scope or is modified further
You'll need to copy the contents of the string x to a new memory area outside x. This external buffer could be in many places such as another string or character array variable, it may or may not have a different lifetime than x due to being in a different scope (e.g. namespace, global, static, heap, shared memory, memory mapped file).
To copy the text from std::string x into an independent character array:
// USING ANOTHER STRING - AUTO MEMORY MANAGEMENT, EXCEPTION SAFE
std::string old_x = x;
// - old_x will not be affected by subsequent modifications to x...
// - you can use `&old_x[0]` to get a writable char* to old_x's textual content
// - you can use resize() to reduce/expand the string
// - resizing isn't possible from within a function passed only the char* address
std::string old_x = x.c_str(); // old_x will terminate early if x embeds NUL
// Copies ASCIIZ data but could be less efficient as it needs to scan memory to
// find the NUL terminator indicating string length before allocating that amount
// of memory to copy into, or more efficient if it ends up allocating/copying a
// lot less content.
// Example, x == "ab\0cd" -> old_x == "ab".
// USING A VECTOR OF CHAR - AUTO, EXCEPTION SAFE, HINTS AT BINARY CONTENT, GUARANTEED CONTIGUOUS EVEN IN C++03
std::vector<char> old_x(x.data(), x.data() + x.size()); // without the NUL
std::vector<char> old_x(x.c_str(), x.c_str() + x.size() + 1); // with the NUL
// USING STACK WHERE MAXIMUM SIZE OF x IS KNOWN TO BE COMPILE-TIME CONSTANT "N"
// (a bit dangerous, as "known" things are sometimes wrong and often become wrong)
char y[N + 1];
strcpy(y, x.c_str());
// USING STACK WHERE UNEXPECTEDLY LONG x IS TRUNCATED (e.g. Hello\0->Hel\0)
char y[N + 1];
strncpy(y, x.c_str(), N); // copy at most N, zero-padding if shorter
y[N] = '\0'; // ensure NUL terminated
// USING THE STACK TO HANDLE x OF UNKNOWN (BUT SANE) LENGTH
char* y = alloca(x.size() + 1);
strcpy(y, x.c_str());
// USING THE STACK TO HANDLE x OF UNKNOWN LENGTH (NON-STANDARD GCC EXTENSION)
char y[x.size() + 1];
strcpy(y, x.c_str());
// USING new/delete HEAP MEMORY, MANUAL DEALLOC, NO INHERENT EXCEPTION SAFETY
char* y = new char[x.size() + 1];
strcpy(y, x.c_str());
// or as a one-liner: char* y = strcpy(new char[x.size() + 1], x.c_str());
// use y...
delete[] y; // make sure no break, return, throw or branching bypasses this
// USING new/delete HEAP MEMORY, SMART POINTER DEALLOCATION, EXCEPTION SAFE
// see boost shared_array usage in Johannes Schaub's answer
// USING malloc/free HEAP MEMORY, MANUAL DEALLOC, NO INHERENT EXCEPTION SAFETY
char* y = strdup(x.c_str());
// use y...
free(y);
Other reasons to want a char* or const char* generated from a string
So, above you've seen how to get a (const) char*, and how to make a copy of the text independent of the original string, but what can you do with it? A random smattering of examples...
give "C" code access to the C++ string's text, as in printf("x is '%s'", x.c_str());
copy x's text to a buffer specified by your function's caller (e.g. strncpy(callers_buffer, callers_buffer_size, x.c_str())), or volatile memory used for device I/O (e.g. for (const char* p = x.c_str(); *p; ++p) *p_device = *p;)
append x's text to an character array already containing some ASCIIZ text (e.g. strcat(other_buffer, x.c_str())) - be careful not to overrun the buffer (in many situations you may need to use strncat)
return a const char* or char* from a function (perhaps for historical reasons - client's using your existing API - or for C compatibility you don't want to return a std::string, but do want to copy your string's data somewhere for the caller)
be careful not to return a pointer that may be dereferenced by the caller after a local string variable to which that pointer pointed has left scope
some projects with shared objects compiled/linked for different std::string implementations (e.g. STLport and compiler-native) may pass data as ASCIIZ to avoid conflicts
Use the .c_str() method for const char *.
You can use &mystring[0] to get a char * pointer, but there are a couple of gotcha's: you won't necessarily get a zero terminated string, and you won't be able to change the string's size. You especially have to be careful not to add characters past the end of the string or you'll get a buffer overrun (and probable crash).
There was no guarantee that all of the characters would be part of the same contiguous buffer until C++11, but in practice all known implementations of std::string worked that way anyway; see Does “&s[0]” point to contiguous characters in a std::string?.
Note that many string member functions will reallocate the internal buffer and invalidate any pointers you might have saved. Best to use them immediately and then discard.
C++17
C++17 (upcoming standard) changes the synopsis of the template basic_string adding a non const overload of data():
charT* data() noexcept;
Returns: A pointer p such that p + i == &operator for each i in [0,size()].
CharT const * from std::basic_string<CharT>
std::string const cstr = { "..." };
char const * p = cstr.data(); // or .c_str()
CharT * from std::basic_string<CharT>
std::string str = { "..." };
char * p = str.data();
C++11
CharT const * from std::basic_string<CharT>
std::string str = { "..." };
str.c_str();
CharT * from std::basic_string<CharT>
From C++11 onwards, the standard says:
The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().
const_reference operator[](size_type pos) const; reference operator[](size_type pos);
Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object of type CharT with value CharT(); the referenced value shall not be modified.
const charT* c_str() const noexcept;const charT* data() const noexcept;
Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
There are severable possible ways to get a non const character pointer.
1. Use the contiguous storage of C++11
std::string foo{"text"};
auto p = &*foo.begin();
Pro
Simple and short
Fast (only method with no copy involved)
Cons
Final '\0' is not to be altered / not necessarily part of the non-const memory.
2. Use std::vector<CharT>
std::string foo{"text"};
std::vector<char> fcv(foo.data(), foo.data()+foo.size()+1u);
auto p = fcv.data();
Pro
Simple
Automatic memory handling
Dynamic
Cons
Requires string copy
3. Use std::array<CharT, N> if N is compile time constant (and small enough)
std::string foo{"text"};
std::array<char, 5u> fca;
std::copy(foo.data(), foo.data()+foo.size()+1u, fca.begin());
Pro
Simple
Stack memory handling
Cons
Static
Requires string copy
4. Raw memory allocation with automatic storage deletion
std::string foo{ "text" };
auto p = std::make_unique<char[]>(foo.size()+1u);
std::copy(foo.data(), foo.data() + foo.size() + 1u, &p[0]);
Pro
Small memory footprint
Automatic deletion
Simple
Cons
Requires string copy
Static (dynamic usage requires lots more code)
Less features than vector or array
5. Raw memory allocation with manual handling
std::string foo{ "text" };
char * p = nullptr;
try
{
p = new char[foo.size() + 1u];
std::copy(foo.data(), foo.data() + foo.size() + 1u, p);
// handle stuff with p
delete[] p;
}
catch (...)
{
if (p) { delete[] p; }
throw;
}
Pro
Maximum 'control'
Con
Requires string copy
Maximum liability / susceptibility for errors
Complex
Just see this:
string str1("stackoverflow");
const char * str2 = str1.c_str();
However, note that this will return a const char *.
For a char *, use strcpy to copy it into another char array.
I am working with an API with a lot of functions that get a char* as an input.
I have created a small class to face this kind of problem, and I have implemented the RAII idiom.
class DeepString
{
DeepString(const DeepString& other);
DeepString& operator=(const DeepString& other);
char* internal_;
public:
explicit DeepString( const string& toCopy):
internal_(new char[toCopy.size()+1])
{
strcpy(internal_,toCopy.c_str());
}
~DeepString() { delete[] internal_; }
char* str() const { return internal_; }
const char* c_str() const { return internal_; }
};
And you can use it as:
void aFunctionAPI(char* input);
// other stuff
aFunctionAPI("Foo"); //this call is not safe. if the function modified the
//literal string the program will crash
std::string myFoo("Foo");
aFunctionAPI(myFoo.c_str()); //this is not compiling
aFunctionAPI(const_cast<char*>(myFoo.c_str())); //this is not safe std::string
//implement reference counting and
//it may change the value of other
//strings as well.
DeepString myDeepFoo(myFoo);
aFunctionAPI(myFoo.str()); //this is fine
I have called the class DeepString because it is creating a deep and unique copy (the DeepString is not copyable) of an existing string.
char* result = strcpy((char*)malloc(str.length()+1), str.c_str());
Converting from c++ std string to C style string is really easy now.
For that we have string::copy function which will easily convert std string to C style string. reference
string::copy functions parameters serially
char string pointer
string size, how many characters will b copied
position, from where character copy will start
Another important thing,
This function does not append a null character at the end of operation. So, we need to put it manually.
Code exam are in below -
// char string
char chText[20];
// c++ string
string text = "I am a Programmer";
// conversion from c++ string to char string
// this function does not append a null character at the end of operation
text.copy(chText, text.size(), 0);
// we need to put it manually
chText[text.size()] = '\0';
// below statement prints "I am a Programmer"
cout << chText << endl;
Vice Versa, Converting from C style string to C++ std string is lot more easier
There is three ways we can convert from C style string to C++ std string
First one is using constructor,
char chText[20] = "I am a Programmer";
// using constructor
string text(chText);
Second one is using string::assign method
// char string
char chText[20] = "I am a Programmer";
// c++ string
string text;
// convertion from char string to c++ string
// using assign function
text.assign(chText);
Third one is assignment operator(=), in which string class uses operator overloading
// char string
char chText[20] = "I am a Programmer";
// c++ string
// convertion from char string to c++ string using assignment operator overloading
string text = chText;
third one can be also write like below -
// char string
char chText[20] = "I am a Programmer";
// c++ string
string text;
// convertion from char string to c++ string
text = chText;
let's say,
string str="stack";
1)converting string to char*
char* s_rw=&str[0];
The above char*(i.e., s_rw) is readable and writeable and points to the base
address of the string which needs to be converted to char*
2)Converting string to const char*
const char* s_r=&str[0];
The above const char* (i.e., s_r) is readable but not writeable and points to the
base address of the string.
This is especially useful when passing the underlying char* buffer of a std::string to C calls which expect and write to a char* buffer. This way you get the best of both worlds!: the niceties of the C++ std::string and the usability of it directly with C libraries you are calling from C++.
How to use a modern C++ std::string as a C-style read/writable char* or read-only null-terminated const char*
How can I convert a std::string to a char* or a const char*?
Despite being a really old and highly-upvoted question, the information I'm about to cover isn't already well-covered, if covered at all, so this is a necessary addition, in particular the part about needing to pre-allocate the underlying C-string using the .resize() method if you'd like to use it as a writable buffer.
All of the usages below require C++11 or later, except for the char* data() call, which requires C++17 or later.
To run and test all example code below, and more, see and run my string__use_std_string_as_a_c_str_buffer.cpp file in my eRCaGuy_hello_world repo.
Quick summary:
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
// IMPORTANT: pre-allocate the underlying buffer to guarantee what size it is
str.resize(BUFFER_SIZE);
// -----------------------------------------------------------------------------
// Get read-writeable access to the underlying `char*` C-string at index i
// -----------------------------------------------------------------------------
char* c_str1 = &str[i]; // <=== my favorite!
char* c_str2 = str.data() + i;
char* c_str3 = &(*str.begin()) + i;
// NB: the C-strings above are NOT guaranteed to be null-terminated, so manually
// write in a null terminator at the index location where you want it if
// desired. Ex:
//
// 1. write a null terminator at some arbitrary position you choose (index 10
// here)
c_str1[10] = '\0';
// 2. write a null terminator at the last guaranteed valid position in the
// underlying C-string/array of chars
c_str2[str.size() - i - 1] = '\0';
// -----------------------------------------------------------------------------
// Get read-only access to the underlying `const char*` C-string at index i
// -----------------------------------------------------------------------------
const char* const_c_str1 = &str[i];
const char* const_c_str2 = str.c_str() + i; // guaranteed to be null-terminated,
// but not necessarily at the
// position you desire; the
// guaranteed null terminator will
// be at index location
// `str.size()`
Summary:
If in a hurry and you need:
A read-writable char* C-string of the underlying buffer: just use section A Technique 1 in the code example just below: char* c_str1 = &str[i];.
Just be sure to pre-allocate the underlying buffer size first via str.resize(BUFFER_SIZE), if needed, is all, to ensure the underlying buffer is big enough for your needs.
A read-only const char* C-string of the underlying buffer: use the same thing as above (const char* const_c_str1 = &str[i];), or const char* const_c_str1 = str.c_str() + i;.
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
// IMPORTANT: pre-allocate the underlying buffer to guarantee what size it is
str.resize(BUFFER_SIZE);
// =============================================================================
// Now you can use the `std::string`'s underlying buffer directly as a C-string
// =============================================================================
// ---------------------------------------------------------
// A. As a read-writeable `char*` C-string
// ---------------------------------------------------------
// Technique 1 [best option if using C++11]: array indexing using `operator[]`
// to obtain a char, followed by obtaining its address with `&`
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/operator_at
char* c_str1 = &str[0];
char* c_str2 = &str[10];
char* c_str3 = &str[33];
// etc.
// Technique 2 [best option if using C++17]: use the `.data()` method to obtain
// a `char*` directly.
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/data
char* c_str11 = str.data(); // same as c_str1 above
char* c_str12 = str.data() + 10; // same as c_str2 above
char* c_str13 = str.data() + 33; // same as c_str3 above
// Technique 3 [fine in C++11 or later, but is awkward, so don't do this. It is
// for demonstration and learning purposes only]: use the `.begin()` method to
// obtain an iterator to the first char, and then use the iterator's
// `operator*()` dereference method to obtain the iterator's `char`
// `value_type`, and then take the address of that to obtain a `char*`
// - Documentation:
// - https://en.cppreference.com/w/cpp/string/basic_string/begin
// - https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
char* c_str21 = &(*str.begin()); // same as c_str1 and c_str11 above
char* c_str22 = &(*str.begin()) + 10; // same as c_str2 and c_str12 above
char* c_str23 = &(*str.begin()) + 33; // same as c_str3 and c_str13 above
// ---------------------------------------------------------
// B. As a read-only, null-terminated `const char*` C-string
// ---------------------------------------------------------
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/c_str
const char* const_c_str1 = str.c_str(); // a const version of c_str1 above
const char* const_c_str2 = str.c_str() + 10; // a const version of c_str2 above
const char* const_c_str3 = str.c_str() + 33; // a const version of c_str3 above
Note that you can also use the .at(i) and .front() std::string methods too, but I won't go into those since I think my examples are sufficient. For their documentation, see:
https://en.cppreference.com/w/cpp/string/basic_string/at
https://en.cppreference.com/w/cpp/string/basic_string/front
Details:
See also the note just above. I'm not going to cover the techniques using the .at(i) and .front() std::string methods, since I think the several techniques I already present are sufficient.
1. Use a std::string as a read/writable char*
To use a C++ std::string as a C-style writable char* buffer, you MUST first pre-allocate the string's internal buffer to change its .size() by using .resize(). Note that using .reserve() to increase only the .capacity() is NOT sufficient! The cppreference.com community wiki page for std::string::operator[] correctly states:
If pos > size(), the behavior is undefined.
The resize() method is what changes the size, not the reserve() method, which changes only the capacity().
Ex:
#include <cstring> // `strcpy()`
#include <iostream>
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
str.resize(BUFFER_SIZE); // pre-allocate the underlying buffer
// check the size
std::cout << "str.size() = " << str.size() << "\n";
For all examples below, assume you have these C-strings:
constexpr char cstr1[] = "abcde ";
constexpr char cstr2[] = "fghijk";
Once you have pre-allocated an underlying buffer which is sufficiently large with resize(), you can then access the underlying buffer as
a char* in at least 3 ways:
Technique 1 [best option if using C++11]: array indexing using operator[] to obtain a char, followed by obtaining its address with &. Ex:
char* c_str;
c_str = &str[0];
c_str = &str[5];
// etc.
// Write these 2 C-strings into a `std::string`'s underlying buffer
strcpy(&str[0], cstr1);
strcpy(&str[sizeof(cstr1) - 1], cstr2); // `- 1` to overwrite the first
// null terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
What if you have a pointer to a std::string? If you have a ptr to a std::string, it must be dereferenced first with *pstr before you can index into it as an array with the operator[] as &(*pstr)[0], so the syntax above becomes a little more awkward. Here is a full example:
std::string str2;
std::string* pstr = &str2;
pstr->resize(BUFFER_SIZE);
c_str = &(*pstr)[0]; // <=== dereference the ptr 1st before indexing into it
// Or, to make the order of precedence
// (https://en.cppreference.com/w/cpp/language/operator_precedence) really
// obvious, you can optionally add extra parenthesis like this:
c_str = &((*pstr)[0]);
Technique 2 [best option if using C++17]: use the .data() method to obtain a char* directly. Ex:
char* c_str;
c_str = str.data();
c_str = str.data() + 5;
// etc.
// Write these 2 C-strings into the `std::string`'s underlying buffer
strcpy(str.data(), cstr1);
strcpy(str.data() + (sizeof(cstr1) - 1), cstr2); // `- 1` to overwrite the
// first null terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
Technique 3 [fine in C++11 and later, but is awkward, so don't do this. It is for demonstration and learning purposes only]: use the .begin() method to obtain an iterator to the first char, and then use the iterator's operator*() dereference method to obtain the iterator's char value_type, and then take the address of that to obtain a char*. Ex:
char* c_str;
c_str = &(*str.begin());
c_str = &(*str.begin()) + 5;
// etc.
// Write these 2 C-strings into the `std::string`'s underlying buffer
strcpy(&(*str.begin()), cstr1);
strcpy(&(*str.begin()) + (sizeof(cstr1) - 1), cstr2); // `- 1` to overwrite
// the first null
// terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
Something important to be aware of is that when you call str.resize(100), it reserves at least 100 bytes for the underlying string, sets the size() of the string to 100, and initializes all 100 of those chars to char()--AKA: the default value initialization value for char (see my question here), which is the binary zero null-terminator, '\0'. Therefore, whenever you call str.size() it will return 100 even if the string simply has "hello" in it followed by 95 null-terminators, or zeros. To get the length, or number of non-null-terminators in the string, you'll have to resort to the C function strlen(), like this:
std::cout << strlen(str.c_str()) << "\n"; // prints `12` in the examples above
// instead of:
std::cout << str.size() << "\n"; // prints `100` in the examples above
2. Access a std::string as a read-only, null-terminated const char*
To obtain a readable null-terminated const char* from a std::string, use the .c_str() method. It returns a C-style string that is guaranteed to be null-terminated. Note that the .data() method is NOT the same thing, as it is NOT guaranteed to be null-terminated!
Example:
std::string str = "hello world";
printf("%s\n", str.c_str());
References
(questions on Stack Overflow)
How to convert a std::string to const char* or char*:
How to convert a std::string to const char* or char*
Directly write into char* buffer of std::string:
Directly write into char* buffer of std::string
Is there a way to get std:string's buffer:
Is there a way to get std:string's buffer
(my content)
[my test code] string__use_std_string_as_a_c_str_buffer.cpp
[my Q] See the "Adjacently related" section at the bottom of my question here:
What is a call to `char()`, `uint8_t()`, `int64_t()`, integer `T()`, etc, as a function in C++?
*****+ [my comments about pre-allocating a buffer in the std::string]:
Directly write into char* buffer of std::string
*****+ [my comment on how to pre-allocate storage in a std::string, to be used as a char* buffer]
Is there a way to get std:string's buffer
(from the cppreference.com community wiki)
https://en.cppreference.com/w/cpp/string/basic_string:
The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin
() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s[0] can
be passed to functions that expect a pointer to the first element of a null-terminated
(since C++11)CharT[] array.
https://en.cppreference.com/w/cpp/string/basic_string/operator_at
Returns a reference to the character at specified location pos. No bounds checking is performed.
If pos > size(), the behavior is undefined.
https://en.cppreference.com/w/cpp/string/basic_string/resize
https://en.cppreference.com/w/cpp/string/basic_string/reserve
https://en.cppreference.com/w/cpp/string/basic_string/data
https://en.cppreference.com/w/cpp/string/basic_string/c_str
https://en.cppreference.com/w/cpp/string/basic_string/clear
Try this
std::string s(reinterpret_cast<const char *>(Data), Size);
This question already has answers here:
Can I get a non-const C string back from a C++ string?
(14 answers)
Closed 5 years ago.
I have a function in a library that takes in a char* and modifies the data.
I tried to give it the c_str() but c++ docs say it returns a const char*.
What can I do other than newing a char array and copying it into that?
You can use &str[0] or &*str.begin() as long as:
you preallocate explicitly all the space needed for the function with resize();
the function does not try to exceed the preallocated buffer size (you should pass str.size() as the argument for the buffer size);
when the function returns, you explicitly trim the string at the first \0 character you find, otherwise str.size() will return the "preallocated size" instead of the "logical" string size.
Notice: this is guaranteed to work in C++11 (where strings are guaranteed to be contiguous), but not in previous revisions of the standard; still, no implementation of the standard library that I know of ever did implement std::basic_string with noncontiguous storage.
Still, if you want to go safe, use std::vector<char> (guaranteed to be contiguous since C++03); initialize with whatever you want (you can copy its data from a string using the constructor that takes two iterators, adding a null character in the end), resize it as you would do with std::string and copy it back to a string stopping at the first \0 character.
Nothing.
Because std::string manages itself its contents, you can't have write access to the string's underlying data. That's undefined behavior.
However, creating and copying a char array is not hard:
std::string original("text");
std::vector<char> char_array(original.begin(), original.end());
char_array.push_back(0);
some_function(&char_array[0]);
If you know that the function will not modify beyond str.size() you can obtain a pointer in one of different ways:
void f( char* p, size_t s ); // update s characters in p
int main() {
std::string s=...;
f( &s[0], s.size() );
f( &s.front(), s.size() );
}
Note, this is guaranteed in C++11, but not in previous versions of the standard where it allowed for rope implementations (i.e. non-contiguous memory)
If your implementation will not try to increase the length of the string then:
C++11:
std::string data = "This is my string.";
func(&*data.begin());
C++03:
std::string data = "This is my string.";
std::vector<char> arr(data.begin(), data.end());
func(&arr[0]);
Here's a class that will generate a temporary buffer and automatically copy it to the string when it's destroyed.
class StringBuffer
{
public:
StringBuffer(std::string & str) : m_str(str)
{
m_buffer.push_back(0);
}
~StringBuffer()
{
m_str = &m_buffer[0];
}
char * Size(int maxlength)
{
m_buffer.resize(maxlength + 1, 0);
return &m_buffer[0];
}
private:
std::string & m_str;
std::vector<char> m_buffer;
};
And here's how you would use it:
// this is from a crusty old API that can't be changed
void GetString(char * str, int maxlength);
std::string mystring;
GetString(StringBuffer(mystring).Size(MAXLEN), MAXLEN);
If you think you've seen this code before, it's because I copied it from a question I wrote: Guaranteed lifetime of temporary in C++?
How can I convert an std::string to a char* or a const char*?
If you just want to pass a std::string to a function that needs const char *, you can use .c_str():
std::string str;
const char * c = str.c_str();
And if you need a non-const char *, call .data():
std::string str;
char * c = str.data();
.data() was added in C++17. Before that, you can use &str[0].
Note that if the std::string is const, .data() will return const char * instead, like .c_str().
The pointer becomes invalid if the string is destroyed or reallocates memory.
The pointer points to a null-terminated string, and the terminator doesn't count against str.size(). You're not allowed to assign a non-null character to the terminator.
Given say...
std::string x = "hello";
Getting a `char *` or `const char*` from a `string`
How to get a character pointer that's valid while x remains in scope and isn't modified further
C++11 simplifies things; the following all give access to the same internal string buffer:
const char* p_c_str = x.c_str();
const char* p_data = x.data();
char* p_writable_data = x.data(); // for non-const x from C++17
const char* p_x0 = &x[0];
char* p_x0_rw = &x[0]; // compiles iff x is not const...
All the above pointers will hold the same value - the address of the first character in the buffer. Even an empty string has a "first character in the buffer", because C++11 guarantees to always keep an extra NUL/0 terminator character after the explicitly assigned string content (e.g. std::string("this\0that", 9) will have a buffer holding "this\0that\0").
Given any of the above pointers:
char c = p[n]; // valid for n <= x.size()
// i.e. you can safely read the NUL at p[x.size()]
Only for the non-const pointer p_writable_data and from &x[0]:
p_writable_data[n] = c;
p_x0_rw[n] = c; // valid for n <= x.size() - 1
// i.e. don't overwrite the implementation maintained NUL
Writing a NUL elsewhere in the string does not change the string's size(); string's are allowed to contain any number of NULs - they are given no special treatment by std::string (same in C++03).
In C++03, things were considerably more complicated (key differences highlighted):
x.data()
returns const char* to the string's internal buffer which wasn't required by the Standard to conclude with a NUL (i.e. might be ['h', 'e', 'l', 'l', 'o'] followed by uninitialised or garbage values, with accidental accesses thereto having undefined behaviour).
x.size() characters are safe to read, i.e. x[0] through x[x.size() - 1]
for empty strings, you're guaranteed some non-NULL pointer to which 0 can be safely added (hurray!), but you shouldn't dereference that pointer.
&x[0]
for empty strings this has undefined behaviour (21.3.4)
e.g. given f(const char* p, size_t n) { if (n == 0) return; ...whatever... } you mustn't call f(&x[0], x.size()); when x.empty() - just use f(x.data(), ...).
otherwise, as per x.data() but:
for non-const x this yields a non-const char* pointer; you can overwrite string content
x.c_str()
returns const char* to an ASCIIZ (NUL-terminated) representation of the value (i.e. ['h', 'e', 'l', 'l', 'o', '\0']).
although few if any implementations chose to do so, the C++03 Standard was worded to allow the string implementation the freedom to create a distinct NUL-terminated buffer on the fly, from the potentially non-NUL terminated buffer "exposed" by x.data() and &x[0]
x.size() + 1 characters are safe to read.
guaranteed safe even for empty strings (['\0']).
Consequences of accessing outside legal indices
Whichever way you get a pointer, you must not access memory further along from the pointer than the characters guaranteed present in the descriptions above. Attempts to do so have undefined behaviour, with a very real chance of application crashes and garbage results even for reads, and additionally wholesale data, stack corruption and/or security vulnerabilities for writes.
When do those pointers get invalidated?
If you call some string member function that modifies the string or reserves further capacity, any pointer values returned beforehand by any of the above methods are invalidated. You can use those methods again to get another pointer. (The rules are the same as for iterators into strings).
See also How to get a character pointer valid even after x leaves scope or is modified further below....
So, which is better to use?
From C++11, use .c_str() for ASCIIZ data, and .data() for "binary" data (explained further below).
In C++03, use .c_str() unless certain that .data() is adequate, and prefer .data() over &x[0] as it's safe for empty strings....
...try to understand the program enough to use data() when appropriate, or you'll probably make other mistakes...
The ASCII NUL '\0' character guaranteed by .c_str() is used by many functions as a sentinel value denoting the end of relevant and safe-to-access data. This applies to both C++-only functions like say fstream::fstream(const char* filename, ...) and shared-with-C functions like strchr(), and printf().
Given C++03's .c_str()'s guarantees about the returned buffer are a super-set of .data()'s, you can always safely use .c_str(), but people sometimes don't because:
using .data() communicates to other programmers reading the source code that the data is not ASCIIZ (rather, you're using the string to store a block of data (which sometimes isn't even really textual)), or that you're passing it to another function that treats it as a block of "binary" data. This can be a crucial insight in ensuring that other programmers' code changes continue to handle the data properly.
C++03 only: there's a slight chance that your string implementation will need to do some extra memory allocation and/or data copying in order to prepare the NUL terminated buffer
As a further hint, if a function's parameters require the (const) char* but don't insist on getting x.size(), the function probably needs an ASCIIZ input, so .c_str() is a good choice (the function needs to know where the text terminates somehow, so if it's not a separate parameter it can only be a convention like a length-prefix or sentinel or some fixed expected length).
How to get a character pointer valid even after x leaves scope or is modified further
You'll need to copy the contents of the string x to a new memory area outside x. This external buffer could be in many places such as another string or character array variable, it may or may not have a different lifetime than x due to being in a different scope (e.g. namespace, global, static, heap, shared memory, memory mapped file).
To copy the text from std::string x into an independent character array:
// USING ANOTHER STRING - AUTO MEMORY MANAGEMENT, EXCEPTION SAFE
std::string old_x = x;
// - old_x will not be affected by subsequent modifications to x...
// - you can use `&old_x[0]` to get a writable char* to old_x's textual content
// - you can use resize() to reduce/expand the string
// - resizing isn't possible from within a function passed only the char* address
std::string old_x = x.c_str(); // old_x will terminate early if x embeds NUL
// Copies ASCIIZ data but could be less efficient as it needs to scan memory to
// find the NUL terminator indicating string length before allocating that amount
// of memory to copy into, or more efficient if it ends up allocating/copying a
// lot less content.
// Example, x == "ab\0cd" -> old_x == "ab".
// USING A VECTOR OF CHAR - AUTO, EXCEPTION SAFE, HINTS AT BINARY CONTENT, GUARANTEED CONTIGUOUS EVEN IN C++03
std::vector<char> old_x(x.data(), x.data() + x.size()); // without the NUL
std::vector<char> old_x(x.c_str(), x.c_str() + x.size() + 1); // with the NUL
// USING STACK WHERE MAXIMUM SIZE OF x IS KNOWN TO BE COMPILE-TIME CONSTANT "N"
// (a bit dangerous, as "known" things are sometimes wrong and often become wrong)
char y[N + 1];
strcpy(y, x.c_str());
// USING STACK WHERE UNEXPECTEDLY LONG x IS TRUNCATED (e.g. Hello\0->Hel\0)
char y[N + 1];
strncpy(y, x.c_str(), N); // copy at most N, zero-padding if shorter
y[N] = '\0'; // ensure NUL terminated
// USING THE STACK TO HANDLE x OF UNKNOWN (BUT SANE) LENGTH
char* y = alloca(x.size() + 1);
strcpy(y, x.c_str());
// USING THE STACK TO HANDLE x OF UNKNOWN LENGTH (NON-STANDARD GCC EXTENSION)
char y[x.size() + 1];
strcpy(y, x.c_str());
// USING new/delete HEAP MEMORY, MANUAL DEALLOC, NO INHERENT EXCEPTION SAFETY
char* y = new char[x.size() + 1];
strcpy(y, x.c_str());
// or as a one-liner: char* y = strcpy(new char[x.size() + 1], x.c_str());
// use y...
delete[] y; // make sure no break, return, throw or branching bypasses this
// USING new/delete HEAP MEMORY, SMART POINTER DEALLOCATION, EXCEPTION SAFE
// see boost shared_array usage in Johannes Schaub's answer
// USING malloc/free HEAP MEMORY, MANUAL DEALLOC, NO INHERENT EXCEPTION SAFETY
char* y = strdup(x.c_str());
// use y...
free(y);
Other reasons to want a char* or const char* generated from a string
So, above you've seen how to get a (const) char*, and how to make a copy of the text independent of the original string, but what can you do with it? A random smattering of examples...
give "C" code access to the C++ string's text, as in printf("x is '%s'", x.c_str());
copy x's text to a buffer specified by your function's caller (e.g. strncpy(callers_buffer, callers_buffer_size, x.c_str())), or volatile memory used for device I/O (e.g. for (const char* p = x.c_str(); *p; ++p) *p_device = *p;)
append x's text to an character array already containing some ASCIIZ text (e.g. strcat(other_buffer, x.c_str())) - be careful not to overrun the buffer (in many situations you may need to use strncat)
return a const char* or char* from a function (perhaps for historical reasons - client's using your existing API - or for C compatibility you don't want to return a std::string, but do want to copy your string's data somewhere for the caller)
be careful not to return a pointer that may be dereferenced by the caller after a local string variable to which that pointer pointed has left scope
some projects with shared objects compiled/linked for different std::string implementations (e.g. STLport and compiler-native) may pass data as ASCIIZ to avoid conflicts
Use the .c_str() method for const char *.
You can use &mystring[0] to get a char * pointer, but there are a couple of gotcha's: you won't necessarily get a zero terminated string, and you won't be able to change the string's size. You especially have to be careful not to add characters past the end of the string or you'll get a buffer overrun (and probable crash).
There was no guarantee that all of the characters would be part of the same contiguous buffer until C++11, but in practice all known implementations of std::string worked that way anyway; see Does “&s[0]” point to contiguous characters in a std::string?.
Note that many string member functions will reallocate the internal buffer and invalidate any pointers you might have saved. Best to use them immediately and then discard.
C++17
C++17 (upcoming standard) changes the synopsis of the template basic_string adding a non const overload of data():
charT* data() noexcept;
Returns: A pointer p such that p + i == &operator for each i in [0,size()].
CharT const * from std::basic_string<CharT>
std::string const cstr = { "..." };
char const * p = cstr.data(); // or .c_str()
CharT * from std::basic_string<CharT>
std::string str = { "..." };
char * p = str.data();
C++11
CharT const * from std::basic_string<CharT>
std::string str = { "..." };
str.c_str();
CharT * from std::basic_string<CharT>
From C++11 onwards, the standard says:
The char-like objects in a basic_string object shall be stored contiguously. That is, for any basic_string object s, the identity &*(s.begin() + n) == &*s.begin() + n shall hold for all values of n such that 0 <= n < s.size().
const_reference operator[](size_type pos) const; reference operator[](size_type pos);
Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object of type CharT with value CharT(); the referenced value shall not be modified.
const charT* c_str() const noexcept;const charT* data() const noexcept;
Returns: A pointer p such that p + i == &operator[](i) for each i in [0,size()].
There are severable possible ways to get a non const character pointer.
1. Use the contiguous storage of C++11
std::string foo{"text"};
auto p = &*foo.begin();
Pro
Simple and short
Fast (only method with no copy involved)
Cons
Final '\0' is not to be altered / not necessarily part of the non-const memory.
2. Use std::vector<CharT>
std::string foo{"text"};
std::vector<char> fcv(foo.data(), foo.data()+foo.size()+1u);
auto p = fcv.data();
Pro
Simple
Automatic memory handling
Dynamic
Cons
Requires string copy
3. Use std::array<CharT, N> if N is compile time constant (and small enough)
std::string foo{"text"};
std::array<char, 5u> fca;
std::copy(foo.data(), foo.data()+foo.size()+1u, fca.begin());
Pro
Simple
Stack memory handling
Cons
Static
Requires string copy
4. Raw memory allocation with automatic storage deletion
std::string foo{ "text" };
auto p = std::make_unique<char[]>(foo.size()+1u);
std::copy(foo.data(), foo.data() + foo.size() + 1u, &p[0]);
Pro
Small memory footprint
Automatic deletion
Simple
Cons
Requires string copy
Static (dynamic usage requires lots more code)
Less features than vector or array
5. Raw memory allocation with manual handling
std::string foo{ "text" };
char * p = nullptr;
try
{
p = new char[foo.size() + 1u];
std::copy(foo.data(), foo.data() + foo.size() + 1u, p);
// handle stuff with p
delete[] p;
}
catch (...)
{
if (p) { delete[] p; }
throw;
}
Pro
Maximum 'control'
Con
Requires string copy
Maximum liability / susceptibility for errors
Complex
Just see this:
string str1("stackoverflow");
const char * str2 = str1.c_str();
However, note that this will return a const char *.
For a char *, use strcpy to copy it into another char array.
I am working with an API with a lot of functions that get a char* as an input.
I have created a small class to face this kind of problem, and I have implemented the RAII idiom.
class DeepString
{
DeepString(const DeepString& other);
DeepString& operator=(const DeepString& other);
char* internal_;
public:
explicit DeepString( const string& toCopy):
internal_(new char[toCopy.size()+1])
{
strcpy(internal_,toCopy.c_str());
}
~DeepString() { delete[] internal_; }
char* str() const { return internal_; }
const char* c_str() const { return internal_; }
};
And you can use it as:
void aFunctionAPI(char* input);
// other stuff
aFunctionAPI("Foo"); //this call is not safe. if the function modified the
//literal string the program will crash
std::string myFoo("Foo");
aFunctionAPI(myFoo.c_str()); //this is not compiling
aFunctionAPI(const_cast<char*>(myFoo.c_str())); //this is not safe std::string
//implement reference counting and
//it may change the value of other
//strings as well.
DeepString myDeepFoo(myFoo);
aFunctionAPI(myFoo.str()); //this is fine
I have called the class DeepString because it is creating a deep and unique copy (the DeepString is not copyable) of an existing string.
char* result = strcpy((char*)malloc(str.length()+1), str.c_str());
Converting from c++ std string to C style string is really easy now.
For that we have string::copy function which will easily convert std string to C style string. reference
string::copy functions parameters serially
char string pointer
string size, how many characters will b copied
position, from where character copy will start
Another important thing,
This function does not append a null character at the end of operation. So, we need to put it manually.
Code exam are in below -
// char string
char chText[20];
// c++ string
string text = "I am a Programmer";
// conversion from c++ string to char string
// this function does not append a null character at the end of operation
text.copy(chText, text.size(), 0);
// we need to put it manually
chText[text.size()] = '\0';
// below statement prints "I am a Programmer"
cout << chText << endl;
Vice Versa, Converting from C style string to C++ std string is lot more easier
There is three ways we can convert from C style string to C++ std string
First one is using constructor,
char chText[20] = "I am a Programmer";
// using constructor
string text(chText);
Second one is using string::assign method
// char string
char chText[20] = "I am a Programmer";
// c++ string
string text;
// convertion from char string to c++ string
// using assign function
text.assign(chText);
Third one is assignment operator(=), in which string class uses operator overloading
// char string
char chText[20] = "I am a Programmer";
// c++ string
// convertion from char string to c++ string using assignment operator overloading
string text = chText;
third one can be also write like below -
// char string
char chText[20] = "I am a Programmer";
// c++ string
string text;
// convertion from char string to c++ string
text = chText;
let's say,
string str="stack";
1)converting string to char*
char* s_rw=&str[0];
The above char*(i.e., s_rw) is readable and writeable and points to the base
address of the string which needs to be converted to char*
2)Converting string to const char*
const char* s_r=&str[0];
The above const char* (i.e., s_r) is readable but not writeable and points to the
base address of the string.
This is especially useful when passing the underlying char* buffer of a std::string to C calls which expect and write to a char* buffer. This way you get the best of both worlds!: the niceties of the C++ std::string and the usability of it directly with C libraries you are calling from C++.
How to use a modern C++ std::string as a C-style read/writable char* or read-only null-terminated const char*
How can I convert a std::string to a char* or a const char*?
Despite being a really old and highly-upvoted question, the information I'm about to cover isn't already well-covered, if covered at all, so this is a necessary addition, in particular the part about needing to pre-allocate the underlying C-string using the .resize() method if you'd like to use it as a writable buffer.
All of the usages below require C++11 or later, except for the char* data() call, which requires C++17 or later.
To run and test all example code below, and more, see and run my string__use_std_string_as_a_c_str_buffer.cpp file in my eRCaGuy_hello_world repo.
Quick summary:
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
// IMPORTANT: pre-allocate the underlying buffer to guarantee what size it is
str.resize(BUFFER_SIZE);
// -----------------------------------------------------------------------------
// Get read-writeable access to the underlying `char*` C-string at index i
// -----------------------------------------------------------------------------
char* c_str1 = &str[i]; // <=== my favorite!
char* c_str2 = str.data() + i;
char* c_str3 = &(*str.begin()) + i;
// NB: the C-strings above are NOT guaranteed to be null-terminated, so manually
// write in a null terminator at the index location where you want it if
// desired. Ex:
//
// 1. write a null terminator at some arbitrary position you choose (index 10
// here)
c_str1[10] = '\0';
// 2. write a null terminator at the last guaranteed valid position in the
// underlying C-string/array of chars
c_str2[str.size() - i - 1] = '\0';
// -----------------------------------------------------------------------------
// Get read-only access to the underlying `const char*` C-string at index i
// -----------------------------------------------------------------------------
const char* const_c_str1 = &str[i];
const char* const_c_str2 = str.c_str() + i; // guaranteed to be null-terminated,
// but not necessarily at the
// position you desire; the
// guaranteed null terminator will
// be at index location
// `str.size()`
Summary:
If in a hurry and you need:
A read-writable char* C-string of the underlying buffer: just use section A Technique 1 in the code example just below: char* c_str1 = &str[i];.
Just be sure to pre-allocate the underlying buffer size first via str.resize(BUFFER_SIZE), if needed, is all, to ensure the underlying buffer is big enough for your needs.
A read-only const char* C-string of the underlying buffer: use the same thing as above (const char* const_c_str1 = &str[i];), or const char* const_c_str1 = str.c_str() + i;.
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
// IMPORTANT: pre-allocate the underlying buffer to guarantee what size it is
str.resize(BUFFER_SIZE);
// =============================================================================
// Now you can use the `std::string`'s underlying buffer directly as a C-string
// =============================================================================
// ---------------------------------------------------------
// A. As a read-writeable `char*` C-string
// ---------------------------------------------------------
// Technique 1 [best option if using C++11]: array indexing using `operator[]`
// to obtain a char, followed by obtaining its address with `&`
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/operator_at
char* c_str1 = &str[0];
char* c_str2 = &str[10];
char* c_str3 = &str[33];
// etc.
// Technique 2 [best option if using C++17]: use the `.data()` method to obtain
// a `char*` directly.
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/data
char* c_str11 = str.data(); // same as c_str1 above
char* c_str12 = str.data() + 10; // same as c_str2 above
char* c_str13 = str.data() + 33; // same as c_str3 above
// Technique 3 [fine in C++11 or later, but is awkward, so don't do this. It is
// for demonstration and learning purposes only]: use the `.begin()` method to
// obtain an iterator to the first char, and then use the iterator's
// `operator*()` dereference method to obtain the iterator's `char`
// `value_type`, and then take the address of that to obtain a `char*`
// - Documentation:
// - https://en.cppreference.com/w/cpp/string/basic_string/begin
// - https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
char* c_str21 = &(*str.begin()); // same as c_str1 and c_str11 above
char* c_str22 = &(*str.begin()) + 10; // same as c_str2 and c_str12 above
char* c_str23 = &(*str.begin()) + 33; // same as c_str3 and c_str13 above
// ---------------------------------------------------------
// B. As a read-only, null-terminated `const char*` C-string
// ---------------------------------------------------------
// - Documentation:
// https://en.cppreference.com/w/cpp/string/basic_string/c_str
const char* const_c_str1 = str.c_str(); // a const version of c_str1 above
const char* const_c_str2 = str.c_str() + 10; // a const version of c_str2 above
const char* const_c_str3 = str.c_str() + 33; // a const version of c_str3 above
Note that you can also use the .at(i) and .front() std::string methods too, but I won't go into those since I think my examples are sufficient. For their documentation, see:
https://en.cppreference.com/w/cpp/string/basic_string/at
https://en.cppreference.com/w/cpp/string/basic_string/front
Details:
See also the note just above. I'm not going to cover the techniques using the .at(i) and .front() std::string methods, since I think the several techniques I already present are sufficient.
1. Use a std::string as a read/writable char*
To use a C++ std::string as a C-style writable char* buffer, you MUST first pre-allocate the string's internal buffer to change its .size() by using .resize(). Note that using .reserve() to increase only the .capacity() is NOT sufficient! The cppreference.com community wiki page for std::string::operator[] correctly states:
If pos > size(), the behavior is undefined.
The resize() method is what changes the size, not the reserve() method, which changes only the capacity().
Ex:
#include <cstring> // `strcpy()`
#include <iostream>
#include <string>
constexpr size_t BUFFER_SIZE = 100;
std::string str;
str.resize(BUFFER_SIZE); // pre-allocate the underlying buffer
// check the size
std::cout << "str.size() = " << str.size() << "\n";
For all examples below, assume you have these C-strings:
constexpr char cstr1[] = "abcde ";
constexpr char cstr2[] = "fghijk";
Once you have pre-allocated an underlying buffer which is sufficiently large with resize(), you can then access the underlying buffer as
a char* in at least 3 ways:
Technique 1 [best option if using C++11]: array indexing using operator[] to obtain a char, followed by obtaining its address with &. Ex:
char* c_str;
c_str = &str[0];
c_str = &str[5];
// etc.
// Write these 2 C-strings into a `std::string`'s underlying buffer
strcpy(&str[0], cstr1);
strcpy(&str[sizeof(cstr1) - 1], cstr2); // `- 1` to overwrite the first
// null terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
What if you have a pointer to a std::string? If you have a ptr to a std::string, it must be dereferenced first with *pstr before you can index into it as an array with the operator[] as &(*pstr)[0], so the syntax above becomes a little more awkward. Here is a full example:
std::string str2;
std::string* pstr = &str2;
pstr->resize(BUFFER_SIZE);
c_str = &(*pstr)[0]; // <=== dereference the ptr 1st before indexing into it
// Or, to make the order of precedence
// (https://en.cppreference.com/w/cpp/language/operator_precedence) really
// obvious, you can optionally add extra parenthesis like this:
c_str = &((*pstr)[0]);
Technique 2 [best option if using C++17]: use the .data() method to obtain a char* directly. Ex:
char* c_str;
c_str = str.data();
c_str = str.data() + 5;
// etc.
// Write these 2 C-strings into the `std::string`'s underlying buffer
strcpy(str.data(), cstr1);
strcpy(str.data() + (sizeof(cstr1) - 1), cstr2); // `- 1` to overwrite the
// first null terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
Technique 3 [fine in C++11 and later, but is awkward, so don't do this. It is for demonstration and learning purposes only]: use the .begin() method to obtain an iterator to the first char, and then use the iterator's operator*() dereference method to obtain the iterator's char value_type, and then take the address of that to obtain a char*. Ex:
char* c_str;
c_str = &(*str.begin());
c_str = &(*str.begin()) + 5;
// etc.
// Write these 2 C-strings into the `std::string`'s underlying buffer
strcpy(&(*str.begin()), cstr1);
strcpy(&(*str.begin()) + (sizeof(cstr1) - 1), cstr2); // `- 1` to overwrite
// the first null
// terminator
// print the string
std::cout << str << "\n"; // output: `abcde fghijk`
Something important to be aware of is that when you call str.resize(100), it reserves at least 100 bytes for the underlying string, sets the size() of the string to 100, and initializes all 100 of those chars to char()--AKA: the default value initialization value for char (see my question here), which is the binary zero null-terminator, '\0'. Therefore, whenever you call str.size() it will return 100 even if the string simply has "hello" in it followed by 95 null-terminators, or zeros. To get the length, or number of non-null-terminators in the string, you'll have to resort to the C function strlen(), like this:
std::cout << strlen(str.c_str()) << "\n"; // prints `12` in the examples above
// instead of:
std::cout << str.size() << "\n"; // prints `100` in the examples above
2. Access a std::string as a read-only, null-terminated const char*
To obtain a readable null-terminated const char* from a std::string, use the .c_str() method. It returns a C-style string that is guaranteed to be null-terminated. Note that the .data() method is NOT the same thing, as it is NOT guaranteed to be null-terminated!
Example:
std::string str = "hello world";
printf("%s\n", str.c_str());
References
(questions on Stack Overflow)
How to convert a std::string to const char* or char*:
How to convert a std::string to const char* or char*
Directly write into char* buffer of std::string:
Directly write into char* buffer of std::string
Is there a way to get std:string's buffer:
Is there a way to get std:string's buffer
(my content)
[my test code] string__use_std_string_as_a_c_str_buffer.cpp
[my Q] See the "Adjacently related" section at the bottom of my question here:
What is a call to `char()`, `uint8_t()`, `int64_t()`, integer `T()`, etc, as a function in C++?
*****+ [my comments about pre-allocating a buffer in the std::string]:
Directly write into char* buffer of std::string
*****+ [my comment on how to pre-allocate storage in a std::string, to be used as a char* buffer]
Is there a way to get std:string's buffer
(from the cppreference.com community wiki)
https://en.cppreference.com/w/cpp/string/basic_string:
The elements of a basic_string are stored contiguously, that is, for a basic_string s, &*(s.begin
() + n) == &*s.begin() + n for any n in [0, s.size()), or, equivalently, a pointer to s[0] can
be passed to functions that expect a pointer to the first element of a null-terminated
(since C++11)CharT[] array.
https://en.cppreference.com/w/cpp/string/basic_string/operator_at
Returns a reference to the character at specified location pos. No bounds checking is performed.
If pos > size(), the behavior is undefined.
https://en.cppreference.com/w/cpp/string/basic_string/resize
https://en.cppreference.com/w/cpp/string/basic_string/reserve
https://en.cppreference.com/w/cpp/string/basic_string/data
https://en.cppreference.com/w/cpp/string/basic_string/c_str
https://en.cppreference.com/w/cpp/string/basic_string/clear
Try this
std::string s(reinterpret_cast<const char *>(Data), Size);