指针数组即是定义一个数组,每个数组的元素存放的是指针类型的数据。声明:int *ptrAry[LEN](以int为例)。

完整例子如下:

#include <stdio.h>

#define ARYLEN	10

int main(int argc, char const *argv[])
{
  int ary[ARYLEN];
  int *ptrAry[ARYLEN];
  int i;

  for(i = 0; i < ARYLEN; i++) { // a regular array
  }

  for(i = 0; i < ARYLEN; i++) { // an array of pointer
    ptrAry[i] = &ary[i];
  }

  for(i = 0; i < ARYLEN; i++) {
    printf("ary[%d]:%d ptr:%p - ptrAry[%d]:%p deref:%d\n",
      i, ary[i], &ary[i], i, ptrAry[i], *ptrAry[i]);
  }
  return 0;
}

运行结果:

ary[0]:0 ptr:0xffdb3454 - ptrAry[0]:0xffdb3454 deref:0
ary[1]:1 ptr:0xffdb3458 - ptrAry[1]:0xffdb3458 deref:1
ary[2]:2 ptr:0xffdb345c - ptrAry[2]:0xffdb345c deref:2
ary[3]:3 ptr:0xffdb3460 - ptrAry[3]:0xffdb3460 deref:3
ary[4]:4 ptr:0xffdb3464 - ptrAry[4]:0xffdb3464 deref:4
ary[5]:5 ptr:0xffdb3468 - ptrAry[5]:0xffdb3468 deref:5
ary[6]:6 ptr:0xffdb346c - ptrAry[6]:0xffdb346c deref:6
ary[7]:7 ptr:0xffdb3470 - ptrAry[7]:0xffdb3470 deref:7
ary[8]:8 ptr:0xffdb3474 - ptrAry[8]:0xffdb3474 deref:8
ary[9]:9 ptr:0xffdb3478 - ptrAry[9]:0xffdb3478 deref:9