我想將string
轉換爲char
數組,但不是char*
。我知道如何將字符串轉換爲char*
(通過使用malloc
或我將其發佈在我的代碼中的方式) - 但這不是我想要的。我只是想將string
轉換爲char[size]
數組。可能嗎?如何將字符串轉換爲C++中的字符數組?
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
// char to string
char tab[4];
tab[0] = 'c';
tab[1] = 'a';
tab[2] = 't';
tab[3] = '\0';
string tmp(tab);
cout << tmp << "\n";
// string to char* - but thats not what I want
char *c = const_cast<char*>(tmp.c_str());
cout << c << "\n";
//string to char
char tab2[1024];
// ?
return 0;
}
這似乎是C++,不C. –
@FredLarson:OK,編輯,謝謝 –
可能重複的[std :: string到char \ *](http://stackoverflow.com/questions/7352099/stdstring-to-char) – cegprakash