如何复制出整个结构体(结构体整体赋值)
时间:2010-04-07 来源:red_eyed_hare
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h> #define MAXSIZE 1000
typedef struct bao
{
int c;
int a;
char b;
}bao; typedef struct min
{
int c;
int a;
char b;
}min; void main()
{
char *tData;
struct min *w;
struct min u;
struct bao q;
struct bao *p;
p=&q;
w=&u;
q.a =100;
q.b ='a';
q.c =125;
u.a =56;
u.b ='c';
u.c =11;
tData = (char*)malloc((unsigned int)(MAXSIZE)*sizeof(char));
memcpy(tData,(char*)p,sizeof(bao));
printf("%d\n",sizeof(bao));
printf("%d,%d,%d,%d,%d,%d\n",tData[0],tData[1],tData[2],tData[3],tData[4],tData[5]);
printf("%d,%d,%d,%d,%d,%d\n",tData[6],tData[7],tData[8],tData[9],tData[10],tData[11]);
printf("%c\n",tData[8]);
memcpy((char*)w,tData,sizeof(bao));
printf("%d\n",w->a);
printf("%d\n",w->c);
free(tData);
}
#include <string.h>
#include <stdlib.h>
#include <malloc.h> #define MAXSIZE 1000
typedef struct bao
{
int c;
int a;
char b;
}bao; typedef struct min
{
int c;
int a;
char b;
}min; void main()
{
char *tData;
struct min *w;
struct min u;
struct bao q;
struct bao *p;
p=&q;
w=&u;
q.a =100;
q.b ='a';
q.c =125;
u.a =56;
u.b ='c';
u.c =11;
tData = (char*)malloc((unsigned int)(MAXSIZE)*sizeof(char));
memcpy(tData,(char*)p,sizeof(bao));
printf("%d\n",sizeof(bao));
printf("%d,%d,%d,%d,%d,%d\n",tData[0],tData[1],tData[2],tData[3],tData[4],tData[5]);
printf("%d,%d,%d,%d,%d,%d\n",tData[6],tData[7],tData[8],tData[9],tData[10],tData[11]);
printf("%c\n",tData[8]);
memcpy((char*)w,tData,sizeof(bao));
printf("%d\n",w->a);
printf("%d\n",w->c);
free(tData);
}
相关阅读 更多 +