字节内存对齐问题

编译指示强制一字节对齐:

#pragma pack(push,1)
struct s
{
        int a;
        char ;
};
#pragma pack(pop)

一些结构体转字符数组和字符数组转结构体操作:

好像叫结构体序列化
结构体与字符数组相互转换,例如:
typedef struct __STUDENT
{
int iAge;
int iYear;
int iMonth;
}STUDENT,*PSTUDENT;
结构体-->字符串数组 ,方法一
PSTUDENT st = NULL;
st = (PSTUDENT)new char[12];
memset(st,0,12);
st->iAge = 12;
st->iMonth = 14;
st->iYear = 100;
CHAR szBuf[13] = {0};
memcpy(szBuf,st,12);
delete st;
st = NULL;
结构体--->字符串数组 ,方法二
STUDENT st;
st.iAge = 12;
st.IMonth = 12;
st.iYear = 1990;
CHAR szBuf[13] = {0};
memcpy(szBuf,&st,12);
字符串数组--->结构体
PSTUDENT st1 = NULL;
st1 = (PSTUDENT)szBuf;
CString str;
str.Format("%d %d %d",st1->iAge,st1->iMonth,st1->iYear);
MessageBox(str);

参考源:http://bbs.chinaunix.net/thread-1955264-1-1.html

原文地址:https://www.cnblogs.com/wainiwann/p/2550016.html