1. 程式人生 > >陣列與字串、字元指標與其他型別指標、賦值引數、指標引數以及引用引數、函式指標

陣列與字串、字元指標與其他型別指標、賦值引數、指標引數以及引用引數、函式指標

◆陣列和字串
從表面上看,一個字串就是一個字元陣列,但在C++語句中,兩者並不完全相同。
字串是一個以串尾符"\0"結尾的字元型陣列,但任一個字元型陣列不見得必為字串(因為其中可以不包含"\0"結尾字元)。

char string1[7]="China";
char string2[]="China";
char string3[7]={'C','h','i','n','a'};
char string4[7]={'C','h','i','n','a','\0'};
string1的長度為7,賦初值後其前6個元素已被賦值(第6個元素為'\0');
string2的長度為6(第6個元素為'\0');
string3的長度為7,最後沒有串尾符'\0';
string4的長度為7,賦初值後與string1相同。

◆字元指標與其他型別指標的使用區別
讀下列程式:
 1
#include<iostream> 2usingnamespace std;
 3int main()
 4{
 5    char str[10]="abcdefghi";
 6    int a[10]={0,1,2,3,4,5,6,7,8,9};
 7    cout<<"'cout<<str'=>"<<str<<endl;
 8    cout<<"'cout<<a'=>"<<a<<endl;
 9    cout<<"--------------------"<<
endl;
10    cout<<"'cout<<&str'=>"<<&str<<endl;
11    cout<<"'cout<<&a'=>"<<&a<<endl;
12    cout<<"--------------------"<<endl;
13    cout<<"'cout<<(int*)str'=>"<<(int*)str<<endl;
14    cout<<
"'cout<<(double*)a'=>"<<(double*)a<<endl;
15    cout<<"--------------------"<<endl;
16    cout<<"'cout<<str+1'=>"<<str+1<<endl;
17    cout<<"'cout<<a+1'=>"<<a+1<<endl;
18    cout<<"--------------------"<<endl;
19    cout<<"'cout<<&str[1]'=>"<<&str[1]<<endl;
20    cout<<"'cout<<&a[1]'=>"<<&a[1]<<endl;
21    cout<<"--------------------"<<endl;
22    cout<<"'cout<<(int*)(str+1)'=>"<<(int*)(str+1)<<endl;
23    cout<<"'cout<<(double*)(a+1)'=>"<<(double*)(a+1)<<endl;
24    cout<<"--------------------"<<endl;
25    cout<<"'cout<<(double*)(&str[1])'=>"<<(double*)(&str[1])<<endl;
26    cout<<"'cout<<(double*)(&a[1])'=>"<<(double*)(&a[1])<<endl;
27    cout<<"--------------------"<<endl;
28    return0;
29}
30 輸出結果:
 1'cout<<str'=>abcdefghi
 2'cout<<a'=>0X0065FDC4//存放地址,可以不同(下同) 3-------------------------- 4'cout<<&str'=>0X0065FDEC 5'cout<<&a'=>0X0065FDC4 6-------------------------- 7'cout<<(int*)str'=>0X0065FDEC 8'cout<<(double*)a'=>0X0065FDC4 9--------------------------10'cout<<str+1'=>bcdefghi
11'cout<<a+1'=>0X0065FDC812--------------------------13'cout<<&str[1]'=>bcdefghi
14'cout<<&a[1]'=>0X0065FDC815--------------------------16'cout<<(int*)(str+1)'=>0X0065FDED17'cout<<(double*)(a+1)'=>0X0065FDC818--------------------------19'cout<<(double*)(&str[1])'=>0X0065FDED20'cout<<(double*)(&a[1])'=>0X0065FDC821-------------------------- 分析:
字元指標與其他型別的指標(如int型指標)在使用上是有所區別的(字元指標具有其特殊性)。如,輸出字元陣列名即字元指標
,就是輸出字元指標所指向的那一個字串;而輸出int型陣列名即int型指標,就是輸出當前的指標值(一個地址)。但輸出
字元陣列名取地址,或輸出int型陣列名取地址,都可以輸出當前的指標值(一個地址)。另外,若將字元陣列名即字元指標
的型別進行轉換後,輸出的將是當前的指標值(一個地址)。

◆賦值引數、指標引數以及引用引數的使用區別
讀下面程式:
 1#include<iostream> 2usingnamespace std;
 3int myFunc(int i1,int* pi21,int*& pi22,int& ri3,char* str);
 4
 5int main()
 6{
 7    int i=1,a[2]={21,202},*pa=a,b[2]={21,202},*pb=b,r=3456;
 8    char s[]="ABCDEFG";
 9    cout<<"--------In main position 1--------"<<endl;
10    cout<<"i,*pa,*pb,r,s="<<i<<","<<*pa<<","<<*pb<<","11        <<r<<","<<s<<endl;
12    cout<<"a[0],a[1],b[0],b[1]="<<a[0]<<","<<a[1]<<","13        <<b[0]<<","<<b[1]<<endl;
14    int tmp=myFunc(i,pa,pb,r,s);
15    cout<<"--------In main position2 2--------"<<endl;
16    cout<<"i,*pa,*pb,r,s="<<i<<","<<*pa<<","<<*pb<<","17        <<r<<","<<s<<endl;
18    cout<<"a[0],a[1],b[0],b[1]="<<a[0]<<","<<a[1]<<","19        <<b[0]<<","<<b[1]<<endl;
20    cout<<"tmp="<<tmp<<endl;
21    return0;
22}
23int myFunc(int i1,int* pi21,int*& pi22,int& ri3,char* str)
24{
25    cout<<"--------In myFunc position 1-------"<<endl;
26    cout<<"i1,*pi21,*pi22,ri3,str="<<i1<<","<<*pi21<<","27        <<*pi22<<","<<ri3<<","<<str<<endl;
28    i1++;
29    pi21++;
30    pi22++;
31    ri3++;
32    (*str)++;
33    cout<<"--------In myFunc position 2-------"<<endl;
34    cout<<"i1,*pi21,*pi22,ri3,str="<<i1<<","<<*pi21<<","35        <<*pi22<<","<<ri3<<","<<str<<endl;
36    i1++;
37    (*pi21)++;
38    (*pi22)++;
39    ri3++;
40    str++;
41    cout<<"--------In myFunc position 3-------"<<endl;
42    cout<<"i1,*pi21,*pi22,ri3,str="<<i1<<","<<*pi21<<","43        <<*pi22<<","<<ri3<<","<<str<<endl;
44
45    return i1;
46}
47 執行結果為:
--------In main position 1--------
i,
*pa,*pb,r,s=1,21,21,3456,ABCDEFG
a[
0],a[1],b[0],b[1]=21,202,21,202
--------In myFunc position 1-------
i1,
*pi21,*pi22,ri3,str=1,21,21,3456,ABCDEFG
--------In myFunc position 2-------
i1,
*pi21,*pi22,ri3,str=2,202,202,3457,BBCDEFG
--------In myFunc position 3-------
i1,
*pi21,*pi22,ri3,str=3,203,203,3458,BCDEFG
--------In main position2 2--------
i,
*pa,*pb,r,s=1,21,203,3458,BBCDEFG
a[
0],a[1],b[0],b[1]=21,203,21,203