c++ string char* const char*

时间:2023-03-08 22:00:13
#include <iostream>
#include <string>
#include <cstring>
using namespace std; int main()
{
{
string s = "tom and jerry";
const char* c_s = s.c_str();
cout << "---------------" << endl;
cout << c_s << endl;
cout << s << endl;
} {
const char* c_s = "tom and jerry";
string s(c_s);
cout << "---------------" << endl;
cout << c_s << endl;
cout << s << endl;
} {
string s = "tom and jerry";
char* c;
const int len = s.length();
c = new char[len+];
strcpy(c, s.c_str()); //#include <cstring>
cout << "---------------" << endl;
cout << c << endl;
cout << s << endl;
} {
char* c = "tom and jerry";
string s(c);
cout << "---------------" << endl;
cout << c << endl;
cout << s << endl;
} {
const char* c = "tom and jerry";
char* pc = new char[];
strcpy(pc, c);
cout << "---------------" << endl;
cout << c << endl;
cout << pc << endl;
} return ;
}