码迷,mamicode.com
首页 > 编程语言 > 详细

数组指针

时间:2015-01-22 13:13:02      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

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

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

数组指针

标签:

原文地址:http://www.cnblogs.com/wudymand/p/4241097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!