#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//两个结构体变量之间的copy行为
struct Teacher
{
char name[62];//64
int age ; //4
};
//两个结构体变量之间可以copy数据,这个是c编译器给我提供的行为,我们要顺从
void main81()
{
//告诉编译器要分配内存
//第一种方法
struct Teacher t1 = {"dddd", 40};
struct Teacher t2 = {"t2", 50};
struct Teacher * p = NULL;
t2 = t1; //这个是=号操作,不是初始化
p = &t2;
printf("sizeof(t1): %d\n", sizeof(t1));
printf("%s\n", t2.name);
printf("%s\n", p->name);
//用memcpy函数完成两个结构体变量之间的copy
memcpy(&t2, &t1, sizeof(struct Teacher));
printf("%s\n", t2.name);
printf("%s\n", p->name);
//我们自己手工的域赋值
//copyObj01(t1, t2);
system("pause");
}
void copyObj01(struct Teacher from, struct Teacher to)
{
memcpy(&to, &from, sizeof(struct Teacher));
}
void copyObj02(struct Teacher *from, struct Teacher *to)
{
memcpy(to, from, sizeof(struct Teacher));
}
//两个结构体变量之间可以copy数据,这个是c编译器给我提供的行为,我们要顺从
void main01()
{
//告诉编译器要分配内存
//第一种方法
struct Teacher t1 = {"dddd", 40};
struct Teacher t2 = {"t2", 50};
//int a ;
//结构体类型是复杂类型,结构体指针出来,就是一级指针
struct Teacher * p = NULL;
//我们自己手工的域赋值
copyObj01(t1, t2);
copyObj02(&t1, &t2);
printf("%s\n", t2.name);
printf("%s\n", p->name);
system("pause");
}
struct Teacher * creatTArray(int count)
{
int i = 0;
struct Teacher*p1 = (struct Teacher *)malloc(count*sizeof(struct Teacher ));
if (p1 == NULL)
{
return NULL;
}
for (i=0; i
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct Teacher_
{
char name[62];//64
int age;
};
int printArray(struct Teacher_ * pArray,int num)
{
int i=0;
for(i=0;i
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//两个结构体变量之间的copy行为
struct Teacher
{
char name[62];//64
int age ; //4
};
int printtArray(struct Teacher *pArray, int num)
{
int i = 0;
for (i=0; i
进阶2
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//两个结构体变量之间的copy行为
struct AdvTeacher
{
char name[64];//64
char *a_name; //4
int age ; //4
};
int printtArray(struct AdvTeacher *pArray, int num)
{
int i = 0;
for (i=0; i
综合
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//两个结构体变量之间的copy行为
struct AdvAdvTeacher
{
char name[64];//64
char *a_name; //4 //结构体里面套一级指针
int age ; //4
char ** stuname;//
};
int printtArray(struct AdvAdvTeacher *pArray, int num)
{
int i = 0;
int j = 0;
for (i=0; i
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//两个结构体变量之间的copy行为
struct AdvAdvTeacher
{
char name[64];//64
char *a_name; //4 //结构体里面套一级指针
int age ; //4
};
struct AdvAdvTeacher* creatTArray(int count)
{
int i = 0;int j = 0;
struct AdvAdvTeacher*p1 = (struct AdvAdvTeacher *)malloc(count*sizeof(struct AdvAdvTeacher ));
if (p1 == NULL)
{
return NULL;
}
for (i=0; ia_name = (char *)malloc(128);
strcpy(to->a_name, from->a_name);
}
void main01()
{
//struct AdvAdvTeacher t1;
//struct AdvAdvTeacher t2;
struct AdvAdvTeacher* p1 =creatTArray(1);
struct AdvAdvTeacher* p2 =creatTArray(1);
printf("\n请输入age: " );
//scanf("%d", &(p1->age) ) ;
p1->age = 11;
printf("\n请输入名字: " );
//scanf("%s", p1->name);
strcpy(p1->name, "11");
printf("\n请输入别名: " );
//scanf("%s", p1->a_name);
strcpy(p1->a_name ,"2");
(*p2) = (*p1);
freeTArray(p1, 1);
freeTArray(p2, 1);
system("pause");
}
void main()
{
struct AdvAdvTeacher t1;
struct AdvAdvTeacher t2;
t1.age = 11;
t1.a_name = (char *)malloc(100);
strcpy(t1.a_name, "t1111");
// {
// char *p = (char *)malloc(100);
// free(p);
// free(p);
// }
//t2 = t1; //编译器赋值操作 编译器等号=操作
copyObj03(&t1, &t2);
if (t1.a_name != NULL)
free(t1.a_name);
if (t2.a_name != NULL)
free(t2.a_name);
system("pause");
}