#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Teacher
{
char *name;
int age;
}Teacher;
//结构体中嵌套指针,而且动态分配空间
//同类型结构体变量赋值
//不同结构体成员指针变量指向同一块内存
int main(void)
{
Teacher t1;
t1.name = (char *)malloc(30);
strcpy(t1.name, "lily");
t1.age = 22;
Teacher t2;
t2 = t1;
//深拷贝,人为增加内容,重新拷贝一下
t2.name = (char *)malloc(30);
strcpy(t2.name, t1.name);
printf("[t2]%s, %d\n", t2.name, t2.age);
if (t1.name != NULL)
{
free(t1.name);
t1.name = NULL;
}
if (t2.name != NULL)
{
free(t2.name);
t2.name = NULL;
}
return 0;
}
C结构体中的深拷贝
最新推荐文章于 2022-05-29 13:24:20 发布