|
◆数组和字符串 从表面上看,一个字符串就是一个字符数组,但在C++语句中,两者并不完全相同。 字符串是一个以串尾符"\0"结尾的字符型数组,但任一个字符型数组不见得必为字符串(因为其中可以不包含"\0"结尾字符)。
char string1[7]="China";
char string2[]="China";
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) char string3[7]={'C','h','i','n','a'};
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) char string4[7]={'C','h','i','n','a','\0'};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
string1的长度为7,赋初值后其前6个元素已被赋值(第6个元素为'\0'); string2的长度为6(第6个元素为'\0'); string3的长度为7,最后没有串尾符'\0'; string4的长度为7,赋初值后与string1相同。 ◆字符指针与其他类型指针的使用区别 读下列程序:
1 #include<iostream> 2 using namespace std; 3 int main() 4![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 5 char str[10]="abcdefghi"; 6![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) 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 return 0; 29 } 30![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
输出结果:
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'=>0X0065FDC8 12 -------------------------- 13 'cout<<&str[1]'=>bcdefghi 14 'cout<<&a[1]'=>0X0065FDC8 15 -------------------------- 16 'cout<<(int*)(str+1)'=>0X0065FDED 17 'cout<<(double*)(a+1)'=>0X0065FDC8 18 -------------------------- 19 'cout<<(double*)(&str[1])'=>0X0065FDED 20 'cout<<(double*)(&a[1])'=>0X0065FDC8 21 --------------------------
分析: 字符指针与其他类型的指针(如int型指针)在使用上是有所区别的(字符指针具有其特殊性)。如,输出字符数组名即字符指针 ,就是输出字符指针所指向的那一个字符串;而输出int型数组名即int型指针,就是输出当前的指针值(一个地址)。但输出 字符数组名取地址,或输出int型数组名取地址,都可以输出当前的指针值(一个地址)。另外,若将字符数组名即字符指针 的类型进行转换后,输出的将是当前的指针值(一个地址)。 ◆赋值参数、指针参数以及引用参数的使用区别 读下面程序:
1 #include<iostream> 2 using namespace std; 3 int myFunc(int i1,int* pi21,int*& pi22,int& ri3,char* str); 4![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 5 int main() 6![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 7![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) 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 return 0; 22 } 23 int myFunc(int i1,int* pi21,int*& pi22,int& ri3,char* str) 24![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 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![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 45 return i1; 46 } 47![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
运行结果为:
--------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
tmp=3
◆函数指针 读程序:
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 double func1(double x); 5 double func2(double y); 6 double func3(double z); 7 void processing(double x,double(*fpt)(double)); 8![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 9 int main() 10![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 11 double x,res; 12 double (*fp)(double); 13 cout<<"Input a real number:"; 14 cin>>x; 15 fp=func1; 16 res=(*fp)(x); 17 cout<<"res=(*fp)(x)="<<res<<endl; 18 fp=func2; 19 res=(*fp)(x); 20 cout<<"res=(*fp)(x)="<<res<<endl; 21 processing(x,func3); 22 fp=func3; 23 processing(x,fp); 24 return 0; 25 } 26 double func1(double x) 27![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 28 cout<<" ---Call func1: 1/x="<<1/x<<endl; 29 return (1/x); 30 } 31 double func2(double x) 32![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 33 cout<<" ---Call func2: x*x="<<x*x<<endl; 34 return (x*x); 35 } 36 double func3(double x) 37![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 38 cout<<" ---Call func3: sqrt(x)="<<sqrt(x)<<endl; 39 return sqrt(x); 40 } 41 void processing(double x,double (*fpt)(double)) 42![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) { 43 cout<<" ---Call processing"<<endl; 44 double result=(*fpt)(x); 45 cout<<" ---result=(*fpt)(x)="<<result<<endl; 46 } 47![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
结果显示:
Input a real number:5.5
---Call func1: 1/x=0.181818
res=(*fp)(x)=0.181818
---Call func2: x*x=30.25
res=(*fp)(x)=30.25
---Call processing
---Call func3: sqrt(x)=2.34512
---result=(*fpt)(x)=2.34512
---Call processing
---Call func3: sqrt(x)=2.34512
---result=(*fpt)(x)=2.34512
|