C++string字符串和C字符串的转换
#include<iostream>#include<string>intmain(){usingnamespacestd;stringfirst_name,last_n...
#include<iostream>
#include<string>
int main()
{
using namespace std;
string first_name,last_name;
first_name="this is my c string";
char a[20];
strcpy(a,first_name);
cout<<a;
return 0;
}我想问strcpy(a,first_name);这里不是可以把string字符串转换成C字符串a吗??为什么还报错!!!
D:\Microsoft Visual Studio\MyProjects\试题2\lihui.cpp(13) : error C2664: 'strcpy' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
而且改成这种也不行
a=first_name; 展开
#include<string>
int main()
{
using namespace std;
string first_name,last_name;
first_name="this is my c string";
char a[20];
strcpy(a,first_name);
cout<<a;
return 0;
}我想问strcpy(a,first_name);这里不是可以把string字符串转换成C字符串a吗??为什么还报错!!!
D:\Microsoft Visual Studio\MyProjects\试题2\lihui.cpp(13) : error C2664: 'strcpy' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
而且改成这种也不行
a=first_name; 展开
2个回答
展开全部
C格式字符串转string可以直接转
string str;
str = "Hello World!";
string转C格式
string str = "Hello World!";
char *str = str.c_str();
string str;
str = "Hello World!";
string转C格式
string str = "Hello World!";
char *str = str.c_str();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询