随笔 - 8  文章 - 3  trackbacks - 0
<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

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

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

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

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

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

#include <stdio.h>

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 <stdio.h>

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 on 2009-05-31 12:10 Vault 阅读(211) 评论(1)  编辑 收藏 引用

FeedBack:
# re: void*型指针的小小trick 2010-01-11 22:58 yu
for(p=&tt;p< &tt + 1 ;p++ )
这个是编译不过的.

for(p=(int*)&tt;p< (int*)(&tt + 1) ;p++ )  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理