I usually find some functions with char* as its parameter.But I heard that std::string is more recommended in C++. How can I use a std::string object with functions taking char*s as parameters? Till now I have known the c_str(), but it doesn't work when the content of string should be modified.
3 Answers
For that purpose, you use std::string::data(). Returns a pointer to the internal data. Be careful not to free this memory or anything like that as this memory is managed by the string object.
4 Comments
You can use the address of the first element after C++11 like this:
void some_c_function(char* s, int n); // ... std::string s = "some text"; some_c_function(&s[0], s.size()); Before C++11 there was no guarantee that the internal string was stored in a contiguous buffer or that it would be null terminated. In those cases making a copy of the string was the only safe option.
After C++17 (the current standard) you can use this:
some_c_function(s.data(), s.size()); In C++17 a non-const return value of std::string::data() was added in addition to the const version.
5 Comments
some_c_function will leave a '\0' terminated C-style string in the "buffer". The string needs to be resized afterwards, because it's size includes the '\0' and the rest of the trailing fallow "buffer".C function might do. Some legacy functions don' t modify strings they are just lax about using const. Also a lot of functions simply fill the string buffer and return the length filled. There could be an alternative end value (like '\n') etc...'\0' may just be data (as used for input only, for input/output, or output buffer only). It may be just a C-style string for input (so a size should be irrelevant). It may be a C-style string output buffer, so a size-as-a-buffer-limit to prevent overrun would be very important. All depends on the API.c_str() simple. And so when C++11 changed the requirements to require contiguous-ness and null termination, most implementations were already in compliance (that is probably one of the reasons why C++11 made the change in the first place, to make the common practice standardized).Since C++17 std::string::data() returns a pointer to the underlying char array that does allow to modify the contents of the string. However, as usual with strings, you may not write beyond its end. From cppreference:
Modifying the past-the-end null terminator stored at data()+size() to any value other than CharT() has undefined behavior.