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
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
整个逗号表达式的值是最右边的表达式的值。test的输出结果为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
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
string 和 char* 的交互初始化问题:对string初始化时候,可以用char *。对char*初始化的时候不能用string。
string的变量名和char*的指针名都可以用为数组名。