指针变量的当前值应合理#include "stdio.h"int main(int argc, char* argv[]){int a[10],i,*p;p = a;printf("please input 10 int number//n");for(i = 0; i < 10;i++)scanf("%d",p++);printf("//n");for(i = 0; i < 10;i++,p++)printf("%d//t",*p);printf("//n");return 0;}运行结果:please input 10 int number0 1 2 3 4 5 6 7 8 96684216 4199177 1 7867264 7867136 0 -2120996372 5570560 -1073961 840 6684520输出结果和我们希望的不一致,正确的写法应该是:#include "stdio.h"int main(int argc, char* argv[]){int a[10],i,*p;p = a;printf("please input 10 int number//n");for(i = 0; i < 10;i++)scanf("%d",p++);printf("//n");p = a; //使指针位置回到头部,很容易出错for(i = 0; i < 10;i++,p++)printf("%d//t",*p);printf("//n");return 0;}在定义数组时,必须指定数组的大小。下列定义是非法的
int a[],i,*p;p = a; /* p =&a[0]; 同样非法 */例题:按正向和反向打印一个字符串#include #include void main(){char *p1,*p2;p1 = p2 = "That/'s all, for the present";while(*p2 != /'//0/')putchar(*p2++);while(--p2 >= p1)putchar(*p2);putchar(/'//n/');}运行结果:That/'s all, for the presenttneserp eht rof ,lla s/'that