字节内存对齐问题

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

复制代码
#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);
复制代码
posted @ 2022-12-26 11:15  阿风小子  阅读(21)  评论(0编辑  收藏  举报