【发布时间】:2012-10-28 22:42:30
【问题描述】:
我想将string 转换为char 数组,但不是char*。我知道如何将字符串转换为char*(通过使用malloc 或我在代码中发布的方式)——但这不是我想要的。我只是想将string 转换为char[size] 数组。有可能吗?
#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:好的,已编辑,谢谢
-
std::string to char*的可能重复
标签: c++ string type-conversion