这篇文章主要介绍“C语言中的packed含义”,在日常操作中,相信很多人在C语言中的packed含义问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C语言中的packed含义”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

packed的涵义研究

在C中,声明结构体时可以用__attribute__ ((__packed__))来修饰,表示压缩结构体所占用的内存空间,使后一个元素紧贴钱一个元素放置。见下面的示例程序:

#include
#include
struct upack
{
 char s;
 int i;
};
struct packs
{
      char s;
      int i;
}__attribute__ ((__packed__));
int main(int argc, char *argv[])
{
 printf("size of packs is %d and upack is %dn",sizeof(struct packs),sizeof(struct upack));
 printf("size of int is %dn",sizeof(int));
 printf("size of char is %dn",sizeof(char));
 struct upack upk;
 printf("address of upk is %dn",&upk);
 printf("address of upk.s is %dn",&(upk.s));
 printf("address of upk.i is %dn",&(upk.i));
 struct packs pk;
   printf("address of pk is %dn",&pk);
 printf("address of pk.s is %dn",&(pk.s));
 printf("address of pk.i is %dn",&(pk.i));
 system("PAUSE");
 return 0;
}