数组指针

数组指针可以理解为指向数组的指针

int (*p)[];

()的优先级大于[],

int (*p)[7];可以理解为一个包含7个int型元素的数组的指针。所以当p+1后,指针将指向7*int后的地址。

如果定义一个二维数组,例如int array[3][7],一个3行7列的数组。

当p=array后,p+1将过7个元素指向第二行,*(*(p+1)+2))即为第二行的第三个元素,输出结果为10。

以下程序字符串数组的定义为比较常用的用法。

#include <iostream>
using namespace std;
int main()
{
{
int (*p)[7];
int array[3][7]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21};
p=array;
cout<<"array[1][2]="<<array[1][2]<<endl;
cout<<"p[1][2]="<<p[1][2]<<endl;
cout<<"*(*(p+1)+2)="<<*(*(p+1)+2)<<endl;
}
{
char strArray[][4]={"abc","def"};
char (*strP)[4];
strP=strArray;
cout<<"strArray[1]="<<strArray[1]<<endl;
cout<<"strP[1]="<<strP[1]<<endl;
cout<<"*(strP+1)="<<*(strP+1)<<endl;
cout<<"*(*(strP+1)+1)="<<*(*(strP+1)+1)<<endl;
}
}

打印输出结果为:

array[1][2]=10
p[1][2]=10
*(*(p+1)+2)=10
strArray[1]=def
strP[1]=def
*(strP+1)=def
*(*(strP+1)+1)=e

posted on 2015-01-22 11:01  wudymand  阅读(350)  评论(0编辑  收藏  举报

导航