std::string to C string
std::string to C string
- 使用 std::basic_string::c_str 方法將字串轉換為 char 陣列
- 使用 std::vector 容器將字串轉換為 Char 陣列
- 使用指標操作操作將字串轉換為字元陣列
c_str()
- return
const char*
1
2string tmp_string = "This will be converted to char*";
auto c_string = tmp_string.c_str();
string.data()
- A pointer to the c-string representation of the string object’s value.
- The pointer returned may be invalidated by further calls to other member functions that modify the object.
- Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
- This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character (‘\0’) at the end.
1
2string str = "some string" ;
char *cstr = str.data();
- This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character (‘\0’) at the end.
由 const char* 轉成 char*
- 直接修改函式參數的型態定義,但原本函式庫裡的宣告根本不能改。
- 用 const_cast<char*>(cptr),這個雖然可以強制轉換,但若透過轉換後的指標更改常數的值,將會是 undefined behavior。
- 使用上一篇提到的 strcpy(),但小心緩衝區覆蓋,或是使用到不知道指到什麼的指標。
- Declare C a const, i.e. const char *C = …
const char *C = R.c_str();
- Copy the content into space that you have allocated.
1
2
3
4
5char *C = new char[R.size()+1];
std::strcpy(C, R.c_str());
// The second problem is a memory leak: your code assigns C a result of new, but never deletes it. If you use strcpy approach, you need to add
delete[] C;
strncpy
- cstring
char* strncpy(char* destination, const char* source, size_t num);
- Copies the first num characters of source to destination.
- If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
No null-character is implicitly appended at the end of destination if source is longer than num.
- Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).
1
2
3
4
5
6
7
8
9
10char str1[]= "To be or not to be";
char str2[40];
char str3[40];
/* copy to sized buffer (overflow safe): */
strncpy ( str2, str1, sizeof(str2) );
/* partial copy (only 5 chars): */
strncpy ( str3, str2, 5 );
str3[5] = '\0'; /* null character manually added */
vector
- Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).
std::vector<char> cstr(str.c_str(), str.c_str() + str.size() + 1);
reference
- 如何在 C++ 中把字串轉換為 Char 陣列 | D棧 - Delft Stack
- [C] 每天來點字串用法 (3) - from const char* to char*
- c++ - error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive] - Stack Overflow
- strncpy - C++ Reference
- c++ - std::string to char* - Stack Overflow
- string::data - C++ Reference