void*型指针的小小trick

====================================


指针的概念还是有很多tricks的,这里的p如果声明为int*型的指针,则


1) p++ 就是p的地址加上一个int型的地址增量,而


2) p+sizeof(int)则是p的地址加上sizeof(int)个int型大小的地址增量;


====================================


#include


typedef struct test


{


int first;


int second;


}mytype;


int main()


{


mytype tt ;


int *p;


tt.first=100;


tt.second=20;



for(p=&tt;p< &tt + 1 ;p++ )


printf("%d\n",*p);


}


======================================


如果把p声明为void*型的指针,则 p+sizeof(int) 就代表了上面多提到的第一种情况


======================================


#include


typedef struct test


{


int first;


int second;


int third;


}mytype;


int main()


{


mytype tt ;


void *p;


tt.first=100;


tt.second=20;


tt.third = 99;


for(p=&tt;p< &tt + 1 ;p += sizeof(int) )


printf("%d\n",*(int*)p);


}



posted @ 2009-10-14 22:17  doujiu  阅读(154)  评论(0编辑  收藏  举报