登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

结构体的补齐机制  

2012-09-04 00:32:42|  分类: C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#include <stdio.h>

int main()
{
 struct A{
  char a;
  int b;
  long c;
  double d;
  
  };
 struct B{
  char a;
  char b;
  char c;
  char d;
  char f;
  int e;
  
  };
 struct C{
  double a;
  char b;
  int c;
  long d;
  
  };
 
 int sizeA = sizeof(struct A);
 int sizeB = sizeof(struct B);
 int sizeC = sizeof(struct C);
 printf("sizeA=%d\nsizeB=%d\nsizeC=%d\n",sizeA,sizeB,sizeC);
 printf("sizeof(unsigned)=%d\n",sizeof(short int));
 
 printf("%d,%d,%d\n",(unsigned long)(&((struct B *)0)->b),(unsigned long)(&((struct B *)0)->c),(unsigned long)(&((struct B *)0)->d));  //这里打印的是偏移量;
 return 0; 
}

运行一下代码稍微分析一下就知道怎么回事了,这里不述。

 

另附:

struct sa
{
  char a;
  int b;
  short c;
  double d;
};

struct sb
{
  double a;
  int b;
  short c;
  char d;
};

//答案:24,16

  评论这张
 
阅读(400)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018