一维数组例子:
#include <stdio.h>
main(){
int a[] ={1,2,3,4,5,6};
int *p;
printf("============a============/n");
printf("&a=%d/n",&a);
printf(" a=%d/n",a);
printf("*a=%d/n",*a);
printf("============a+0============/n");
printf("&a+0=%d/n",&a+0);
printf(" a+0=%d/n",a+0);
printf("*a+0=%d/n",*a+0);
printf("============a[0]============/n");
printf("&a[0]=%d/n",&a[0]);
printf(" a[0]=%d/n",a[0]);
printf("============old p============/n");
printf("old &p=%d/n",&p);
printf("old p=%d/n",p);
printf("old *p=%d/n",*p);
p = a;
printf("============p============/n");
printf("&p=%d/n",&p);
printf(" p=%d/n",p);
printf("*p=%d/n",*p);
printf("============p[1]============/n");
printf("&p[1]=%d/n",&p[1]);
printf(" p[1]=%d/n",p[1]);
printf("============a+1============/n");
printf("a+1=%d/n",a+1);
printf("p[1]=%d/n",p[1]);
printf("p+1=%d/n",p+1);
printf("*p+1=%d/n",*p+1);
printf("*(p+1=)%d/n",*(p+1));
/*
指针类型(保存地址)占用4空间
void类型(无类型)占用1空间
*/
void *p2;
printf("============old p2============/n");
printf("old &p2=%d/n",&p2);
printf("old p2=%d/n",p2);
int i;
for (i = 0 ;i<10 ; i++){
printf("============p2+%d============/n",i);
printf("&p2+%d=%d/n",i,&p2+i);
printf(" p2+%d=%d/n",i,p2+i);
}
}
二维数组例子:
#include <stdio.h>
main(){
int a[2][3] = {{1,2,3},{4,5,6}};
int *p;
p = NULL;
printf("==========p===========/n");
printf("&p=%d/n",&p);
printf(" p=%d/n",p);
printf("*p=%d/n",&*p);
p = a;
printf("==========p===========/n");
printf("&p=%d/n",&p);
printf(" p=%d/n",p);
printf("*p=%d/n",*p);
int i;
for(i=0;i<6;i++){
printf("==========p+%d===========/n",i);
printf("&p+%d=%d/n",i,&p+i);
printf(" p+%d=%d/n",i,p+i);
printf("*p+%d=%d/n",i,*p+i);
}
int j;
for(i = 0; i<2;i++){
for(j=0;j<3;j++){
printf("==========*(*(a+%d)+%d)===========/n",i,j);
printf("&*(*(a+%d)+%d)=%d/n",i,j,&*(*(a+i)+j));
printf(" *(*(a+%d)+%d)=%d/n",i,j,*(*(a+i)+j));
}
}
/*
定义2维指针数组,每指针指向a数组的一维(一行)
申请空间,赋值。
pointer Array rows=2
prow[0];
prow[1];
*/
int *prow[2];
for(i=0;i<2;i++)prow[i]=a[i];
for(i = 0; i<2;i++){
for(j=0;j<3;j++){
printf("==========*(*(prow+%d)+%d)===========/n",i,j);
printf("&*(*(prow+%d)+%d)=%d/n",i,j,&*(*(prow+i)+j));
printf(" *(*(prow+%d)+%d)=%d/n",i,j,*(*(prow+i)+j));
}
}
/*
定义3维整型数组由pcol指针指向此数组
只定义,没有申请空间,定义成3维数组是为了与a数组保持相同维数,便于递增。
*/
int (*pcol)[3];
pcol = a;
// pcol = &(a[0][0]);
for(i = 0; i<2;i++){
for(j=0;j<3;j++){
printf("==========*(*(pcol+%d)+%d)===========/n",i,j);
printf("&*(*(pcol+%d)+%d)=%d/n",i,j,&*(*(pcol+i)+j));
printf(" *(*(pcol+%d)+%d)=%d/n",i,j,*(*(pcol+i)+j));
}
}
}