当前位置:网站首页>Char[], char *, conversion between strings

Char[], char *, conversion between strings

2022-06-23 01:14:00 ppipp1109

 

char [] And char * Between

char [] turn char *: Assign values directly

// char[] turn char *
char str[] = "lala";
char *str1 = str;
cout << str1 << endl;

char * turn char[]: Character copy implementation , You cannot assign values

// char * Convert to char []
const char *st = "hehe";
char st1[] = "lalalala";
strncpy(st1, st, strlen(st) + 1); // Pay attention to add 1 operation
// tp = temp; // error , Can't achieve
cout << st1 << endl;

char And const char Between

const char turn char : Copy implementation , Cannot assign

// const char * turn char *
const char *st = "lala";
// Direct assignment is not allowed
//char *st1 = st; // ( No compiler error is allowed )
//cout << st1 << endl;
// In addition, open up space , Copy the characters one by one
char *ncstr = new char[strlen(st) + 1];
strcpy(ncstr, st);
cout << ncstr << endl;

char turn const char : Assign values directly

// char * turn const char *
char *st = "hehe"; // ( Compile prompt warning )
const char *st1 = st;
cout << st1 << endl;

char * And string Between

char * turn string:1) Direct assignment ;2) Construct transformation to realize

// char* Convert to string
// ( Be careful , Definition char * Variable , And assign values directly , Best defined as const Variable , Otherwise, the compiler warns )
const char *st = "hello";
// Assignment conversion
string st1 = st;
cout << st1 << endl;
// Structural transformation
string s1(st, st + strlen(st));
cout << s1 << endl;
// change const char * A variable's value
st = "lalala";
cout << st << endl;

string turn char *: Assignment operation ( Pay attention to type conversion )

// string turn char *
string st = "My test";
//char *st1 = st; // Different types of errors
//char *st1 = st.c_str(); // Different types of errors
char *st1 = const_cast<char *>(st.c_str()) ;
cout << st1 << endl;

char[] And string Between

char [] turn string:1) Direct assignment ;2) Construct transformation to realize

// char[] Convert to string
char st[] = "hello";
// Direct assignment to achieve
string st1 = st;
cout << st1 << endl;
// Construct implementation
string st2(st, st + strlen(st));
cout << st2 << endl;

string turn char[]: Copy implementation , It cannot be assigned directly

// string turn char []
string ts = "My test1";
//char ts1[] = ts; // error
//char ts1[] = const_cast<char *>(ts.c_str()); // error
char ts1[] = "lalallalalaaaa";
strncpy(ts1, ts.c_str(), ts.length() + 1); // Be careful , Be sure to add. 1, Otherwise, there is no assignment '\0'
cout << ts1 << endl;
return 0;

summary

involves char [] Character array and other types conversion , Generally, copying is required , It is not possible to assign values directly .char [] and char * By constructing new string Complete their pairs string Transformation . Involving char * transformation , Note that the types are consistent , At the same time pay attention to const Use .

原网站

版权声明
本文为[ppipp1109]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220923108243.html