Conversion (transfer) of string, char *, char [] in C ++

1. String to char *

  There are three main ways to convert str to char * types, namely: data (); c_str (); copy ();

  1. The data () method, such as:  

string str = " hello " ;
 const  char * p = str.data (); // Add const or use char * p = (char *) str.data ();

  At the same time, there is one point that needs to be explained here. Compiling in devc ++ needs to add const, otherwise it will report an invalid conversion from const char * to char *, where you can add const in front or force conversion into char * after the equal sign.

  The following explains the problem. Const char * cannot be directly assigned to char *, so that compilation will not pass. Reason: if possible, then the content pointed to by const char can be modified through char *, which is not allowed. Therefore, char * needs to open up new space, namely the above form.

  

  2.c_str () method, such as:

string str = "world";
 const  char * p = str.c_str (); // Same as above, add const or equal sign to the right with char *

 

  3. The copy () method, such as:

string str = " hmmm " ;
 char p [ 50 ]; 
str.copy (p, 5 , 0 ); // Here 5 stands for copying several characters, 0 stands for copying position, 
* (p + 5 ) = '\ 0 ' ; // Pay attention to manually add terminator! ! !

 

Second, char * to string.

  Can be assigned directly.

string s;
 char * p = " hello " ; // direct assignment 
s = p;

    One thing to note here is that when the string variable s is declared, using printf ("% s", s); will cause an error, because "% s" requires the first address of the following object. But string is not such a type. So it must be wrong.

  

Three, string to char []

  Since we know the length of the string, it can be obtained according to the length () function, and can be directly accessed according to the subscript, so it can be assigned with a loop.

string pp = "dagah";
char p[8];
int i;
for( i=0;i<pp.length();i++)
       p[i] = pp[i];
p[i] = '\0';
printf("%s\n",p);
cout<<p;

 

Four, char [] to string

  Here you can assign directly.

 

Original address: https://www.cnblogs.com/Pillar/p/4206452.html

  There are three main ways to convert str to char * types, namely: data (); c_str (); copy ();

  1. The data () method, such as:  

string str = " hello " ;
 const  char * p = str.data (); // Add const or use char * p = (char *) str.data ();

  At the same time, there is one point that needs to be explained here. Compiling in devc ++ needs to add const, otherwise it will report an invalid conversion from const char * to char *, where you can add const in front or force conversion into char * after the equal sign.

  The following explains the problem. Const char * cannot be directly assigned to char *, so that compilation will not pass. Reason: if possible, then the content pointed to by const char can be modified through char *, which is not allowed. Therefore, char * needs to open up new space, namely the above form.

  

  2.c_str () method, such as:

string str = "world";
 const  char * p = str.c_str (); // Same as above, add const or equal sign to the right with char *

 

  3. The copy () method, such as:

string str = " hmmm " ;
 char p [ 50 ]; 
str.copy (p, 5 , 0 ); // Here 5 stands for copying several characters, 0 stands for copying position, 
* (p + 5 ) = '\ 0 ' ; // Pay attention to manually add terminator! ! !

 

Second, char * to string.

  Can be assigned directly.

string s;
 char * p = " hello " ; // direct assignment 
s = p;

    One thing to note here is that when the string variable s is declared, using printf ("% s", s); will cause an error, because "% s" requires the first address of the following object. But string is not such a type. So it must be wrong.

  

Three, string to char []

  Since we know the length of the string, it can be obtained according to the length () function, and can be directly accessed according to the subscript, so it can be assigned with a loop.

string pp = "dagah";
char p[8];
int i;
for( i=0;i<pp.length();i++)
       p[i] = pp[i];
p[i] = '\0';
printf("%s\n",p);
cout<<p;

 

Four, char [] to string

  Here you can assign directly.

 

Original address: https://www.cnblogs.com/Pillar/p/4206452.html

Guess you like

Origin www.cnblogs.com/kerwincui/p/12730497.html