【程序员面试宝典读书笔记】指针数组和数组指针



指针数组:数组元素全部是指针的数组

数组指针:指向数组的指针

定义说明:

int *ptr[5]; 指针数组,定义了长度为5的一维数组,并且ptr中的元素都是int型指针


int (*ptr)[5];数组指针,定义了指向一维数组ptr的指针,这个一维int型数组长度为5

 

举例说明:

#include<iostream>
#include<stdio.h>
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}};
    int (*a)[10]=v;//数组指针
    cout<<**a<<endl;
    cout<<**(a+1)<<endl;
    cout<<*(*a+1)<<endl;
    cout<<*(a[0]+1)<<endl;
    cout<<*(a[1])<<endl;
    system("pause");
    return 0;
}

以上例子摘自《程序员面试宝典》

分析:定义了指向每行包括10个int元素的二维数组v的指针,a+1表明向后移动1*sizeof(每行数组大小),*a+1仅对这一行向后移动4个字节,如下图所示:


所以结果是1 11 2 2 11


#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[1]=page2;
    cout<<*ptr[0]<<endl;
    cout<<*page<<endl;
    cout<<*ptr[1]<<endl;
    system("pause");
    return 0;
}

以上例子摘自《程序员面试宝典》

分析:指针page,ptr[0]指向p,page2,ptr[1]指向p2

所以结果是5 5 6

posted on 2015-01-26 21:32  ruan875417  阅读(165)  评论(0编辑  收藏  举报

导航