需要定义一个structure,问下面哪个比较好,为什么。
struct A
{
char char1[5];
float fData;
long lData;
char char2[1];
};
struct B
{
long lData;
float fData;
char char1[5];
char char2[1];
};
小提示:这个公司是作实时系统的。
如果仅从占用空间考虑,其实两个都可以做到一样
#include <stdio.h>
#pragma pack(1)
struct A
{
char char1[5];
float fData;
long lData;
char char2[1];
}a;
struct B
{
long lData;
float fData;
char char1[5];
char char2[1];
}b;
#pragma pack()
struct C
{
char char1[5];
float fData;
long lData;
char char2[1];
}c;
struct D
{
long lData;
float fData;
char char1[5];
char char2[1];
}d;
int main()
{
printf("A=%d\n",sizeof(A));
printf("A.char1(%d)+A.fData(%d)+A.lData(%d)+A.char2(%d)\n\n",sizeof(a.char1),sizeof(a.fData),sizeof(a.lData),sizeof(a.char2));
printf("B=%d\n",sizeof(B));
printf("B.char1(%d)+B.fData(%d)+B.lData(%d)+B.char2(%d)\n\n",sizeof(b.char1),sizeof(b.fData),sizeof(b.lData),sizeof(b.char2));
printf("C=%d\n",sizeof(C));
printf("C.char1(%d)+C.fData(%d)+C.lData(%d)+C.char2(%d)\n\n",sizeof(c.char1),sizeof(c.fData),sizeof(c.lData),sizeof(c.char2));
printf("D=%d\n",sizeof(D));
printf("D.char1(%d)+D.fData(%d)+D.lData(%d)+D.char2(%d)\n\n",sizeof(d.char1),sizeof(d.fData),sizeof(d.lData),sizeof(d.char2));
return 0;
}
访问时速度上是否会有区别不得而知