65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string.h>
using namespace std;
void fun(char* pch)
{
cout << pch << endl;
}
int main()
{
string str = "hello";
char *pch = const_cast<char*>(str.c_str());//你报错是因为这里写的不对吧?
fun(pch);
getchar();
return 0;
}
没问题的。
#include <iostream>
#include <string.h>
using namespace std;
void fun(char* pch)
{
cout << pch << endl;
}
int main()
{
string str = "hello";
char *pch = const_cast<char*>(str.c_str());
fun(pch);
getchar();
return 0;
}
没问题的。
#include <iostream>
#include <string.h>
using namespace std;
void fun(char* pch)
{
cout << pch << endl;
}
int main()
{
string str = "hello";
char *pch = const_cast<char*>(str.c_str());
fun(pch);
getchar();
return 0;
}
没问题的。
int lilen = str.length()+1;
char* pszTemp = new char[lilen];
memset(pszTemp, 0, lilen);
memcpy(pszTemp, str.c_str(), lilen-1);