指针数组:数组元素全部是指针的数组
数组指针:指向数组的指针
定义说明:
int *ptr[5]; 指针数组,定义了长度为5的一维数组,并且ptr中的元素都是int型指针
int (*ptr)[5];数组指针,定义了指向一维数组ptr的指针,这个一维int型数组长度为5
一、数组指针
#include <iostream>
using namespace std;
int main()
{
int v[2][10]={{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,18,19,20}};
//定义一个指针a,指针a是一个指向有10个int类型数组的指针
int (*a)[10] = v;//数组指针!!!!!!!
cout<<&v[0][0]<<endl;//0x28febc
cout<<v<<endl;//0x28febc
cout<<*a<<endl;//0x28febc
//1, *a是其第一行第一个元素地址,则**a即是取第一个元素
cout<<**a<<endl;
//11,*(a+1)是其第二行第一个元素地址,则**(a+1)是取第二行第一个元素
cout<<**(a+1)<<endl;
//2, *a+1是其第一行第二个元素地址,则*(*a+1)即是取第一行第二个元素
cout<<*(*a+1)<<endl;//2
//同上
cout<<*(a[0]+1)<<endl;//2
//a[1]同*(a+1), a[0]为第一行第一个,a[1]为第二行第一个
cout<<*(a[1])<<endl;//11
return 0;
}
结果:
0x28febc
0x28febc
0x28febc
1
11
2
2
11
Process returned 0 (0x0) execution time : 0.215 s
Press any key to continue.
二、指针数组
#include<iostream>
using namespace std;
int main()
{
int *ptr[5];//指针数组
int p=5,p2=6,*page,*page2;
page=&p;
page2=&p2;
ptr[0]=&p; //ptr[5]是数组,里面5个元素都是指针
ptr[1]=page2;
cout<<*ptr[0]<<endl;
cout<<*page<<endl;
cout<<*ptr[1]<<endl;
return 0;
}
结果:
5
5
6
Process returned 0 (0x0) execution time : 0.091 s
Press any key to continue.
分析:指针page,ptr[0]指向p,page2,ptr[1]指向p2。