Discussion string variable

For storage strings come in many forms, take a look below.

On the code:

. 1 #include <the iostream>
 2 #include < String >
 . 3  the using  namespace STD;
 . 4  
. 5  int main ()
 . 6  {
 . 7      char * S = " ABCDE " ; // is a variable is assigned to a pointer to a string constant 
. 8      char S1 [] {= " ABCDE " };
 . 9      // char S [. 5] {= "ABCDE"}; 
10      char S2 [ . 6 ] {= " ABCDE " };
 . 11      char S3 [ . 5 ] = { 'A','B','C','D','E'};
12     char s4[6]={'A','B','C','D','E','\0'};
13     string str="ABCDE";
14     string str1=" ABCDE Hello! " ; // string of characters comprises
 15      the puts (S);
 16      COUT << strlen (S) << "  " << the sizeof (S) << endl; // the sizeof calculated here is a byte pointer variable size, 32 bytes at 4 
. 17      the puts (S1);
 18 is      COUT << strlen (S1) << "  " << the sizeof (S1) << endl;
 . 19      the puts (S2);
 20 is      COUT strlen << (S2) << "  " << the sizeof (S2) << endl;
 21 is      the puts (S3);// Since this character string free end, the rear output will be garbled 
22 is      COUT << strlen (S3) << " " << the sizeof (S3) << endl;
 23 is      the puts (S4);
 24      COUT << strlen (S4) << "  " << the sizeof (S4) << endl;
 25      COUT STR << << endl;
 26 is      COUT str.length << () << "  " << the sizeof (STR) << endl; // also be str.size () calculates the length of the string, the calculated number of bytes 
27      COUT << str1.length ( ) << "  " << sizeof (str1) << endl;// look str1 know 
28  
29      System ( " PAUSE " );
 30     return 0;
31 }

Reproduced in: https: //www.cnblogs.com/sjlove/p/3163323.html

Guess you like

Origin blog.csdn.net/weixin_33901641/article/details/94019532