Code involving string, char *, char [], and comma expressions

Source: Internet
Author: User

 

1 # include <iostream>
2 # include <string>
3 using namespace STD;
4
5 void main ()
6 {
7 char * Pc = "Lu Wei ";
8 cout <Pc <Endl;
9
10 char array [] = "mengliangliang ";
11 cout <array <Endl;
12
13 string str1 (PC );
14 string str2 (array );
15 cout <str1 <str2 <Endl;
16
17 string * str3 = new string;
18 int * A = new int (6 );
19 cout <* A <Endl;
20
21 int test = (* A) ++, 5 );
22 cout <test <Endl;
23}
24

The value of the entire comma expression is the value of the rightmost expression. The output result of test is 5.

 

 

1 void fun (char STR [])
2 {
3 // Process Code
4}
5
6 void main ()
7 {
8 string str5 = "Meng ";
9 char * pstr5 = str5; // Error
10 char a_char [] = str5; // Error
11
12 char * pstr6 = "Xiangshan ";
13 string str6 = pstr6; // right
14 char a_ch [] = pstr6; // Error
15 fun (pstr6); // right
16
17 cout <pstr6 [0] <Endl; // right
18 cout <str5 [0] <Endl; // right
19}
20

Interaction initialization between string and char *: char * can be used for string initialization *. String cannot be used for char * initialization.

String variable name and char * pointer name can be used as an array name.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.