Converting String To Cstring In C++


Answer :

.c_str() returns a const char*. If you need a mutable version, you will need to produce a copy yourself.



vector<char> toVector( const std::string& s ) {
string s = "apple";
vector<char> v(s.size()+1);
memcpy( &v.front(), s.c_str(), s.size() + 1 );
return v;
}
vector<char> v = toVector(std::string("apple"));

// what you were looking for (mutable)
char* c = v.data();


.c_str() works for immutable. The vector will manage the memory for you.



Comments

Popular posts from this blog

530 Valid Hostname Is Expected When Setting Up IIS 10 For Multiple Sites

Android - ImageView With Rounded Only One Corner

Android - SetOnClickListener Vs OnClickListener Vs View.OnClickListener