我们喜欢在程序中使用指针代替数组,因为变量指针可以递增,而数组不能递增,因为数组是一个常量指针。下面的程序递增变量指针,以便顺序访问数组中的每一个元素:
#include <stdio.h> const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i, *ptr; /* 指针中的数组地址 */ ptr = var; for ( i = 0; i < MAX; i++) { printf("存储地址:var[%d] = %x ", i, ptr ); printf("存储值:var[%d] = %d ", i, *ptr ); /* 移动到下一个位置 */ ptr++; } return 0; } 当上面的代码被编译和执行时,它会产生下列结果: 存储地址:var[0] = bf882b30 存储值:var[0] = 10 存储地址:of var[1] = bf882b34 存储值: var[1] = 100 存储地址:of var[2] = bf882b38 存储值:var[2] = 200