现在的位置: 首页 > 综合 > 正文

数组与字符串、字符指针与其他类型指针、赋值参数、指针参数以及引用参数、函数指针

2013年10月07日 ⁄ 综合 ⁄ 共 4513字 ⁄ 字号 评论关闭

原文地址:http://www.cppblog.com/xingjiegaojue/archive/2009/08/12/93099.aspx

数组与字符串、字符指针与其他类型指针、赋值参数、指针参数以及引用参数、函数指针

◆数组和字符串
从表面上看,一个字符串就是一个字符数组,但在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>
 2using namespace 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    return 0;
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'=>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>
 2using namespace 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    return 0;
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
tmp
=3

◆函数指针
读程序:

 1#include<iostream>
 2#include<cmath>
 3using namespace std;
 4double func1(double x);
 5double func2(double y);
 6double func3(double z);
 7void processing(

抱歉!评论已关闭.