/************************************************************************* > File Name: memtest.c > Author: > Mail: > Created Time: Thu 27 Dec 2018 05:47:37 PM CST ************************************************************************/ #include<stdio.h> #define N 50 #define IPSIZE 15 struct ClientInfo{ char headName[N]; char name[N]; int num; char ip[IPSIZE]; }; int main(int argc,char **argv) { struct ClientInfo aaa; strcpy(aaa.headName,"aaaaaaaaaaaaa"); strcpy(aaa.name,"bbbbbbbbb"); aaa.num = 10; strcpy(aaa.ip,"ccccccc"); char buf[200]; memcpy(buf,&aaa,sizeof(aaa)); struct ClientInfo bbb; memcpy(&bbb,buf,sizeof(bbb)); printf("%s ",bbb.headName); printf("%s ",bbb.name); printf("%d ",bbb.num); printf("%s ",bbb.ip); }
输出结果:
aaaaaaaaaaaaa
bbbbbbbbb
10
ccccccc
这里要注意结构体的字节对齐规则
后续会把相关知识录入博客