string 转为char
发布网友
发布时间:2022-05-13 00:52
我来回答
共4个回答
热心网友
时间:2023-11-04 07:03
提示错误:error C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
在MSDN中查找c_str()
const E *c_str() const;
The member function returns a pointer to a nonmodifiable C string constructed by adding a terminating null element (E(0)) to the controlled sequence. Calling any non-const member function for *this can invalidate the pointer.
则可以改为:
#include <iostream>
#include <string>
using namespace std;
void main()
{
string a = "2009";
char const *b = a.c_str();
cout << b << endl;
}
热心网友
时间:2023-11-04 07:04
#include<iostream>
#include<string>
using namespace std;
void main(){
string a="2009";
char b[10];
strcpy(b,a.c_str());
cout<<b<<endl;
}
热心网友
时间:2023-11-04 07:04
c_str函数的返回值是const char*的,不能直接赋值给char*.
http://ke.baidu.com/view/1600698.htm
热心网友
时间:2023-11-04 07:05
const char *b=a.c_str();