我接下来会写五篇代码,这些代码包括memcpy的进一步用法、指针的用法,以及结构体,如果你能够看懂,说明你指针的功力已经很深了,
解决大部分问题是OK的,这也是我一步一步思考出来的,也是自我的提升。
1.当结构体指针需要自己存数据,而不是指向别的变量地址,是需要申请内存空间的,不然会发生段错误(你们可以把malloc那段代码去掉自行验证)。
#include<string.h>
#include<stdlib.h>
struct Student
{
int age;
char name[256];
int ID;
};
int main()
{
struct Student *stu1;
struct Student *stu2;
stu1 = (struct Student*)malloc(sizeof(struct Student));
stu2 = (struct Student*)malloc(sizeof(struct Student));
stu1->age = 20;
strcpy(stu1->name,"zhangsan");
stu1->ID = 123456;
memcpy(stu2,stu1,sizeof(struct Student));
printf("stu2->age=%d,stu2->name=%s,stu2->ID=%d\n",stu2->age,stu2->name,stu2->ID);
memset(stu2,0,sizeof(struct Student));
printf("stu2->age=%d,stu2->name=%s,stu2->ID=%d\n",stu2->age,stu2->name,stu2->ID);
return 0;
}
2.当在main函数传入结构体指针,将stu1的值复制给stu2,stu2也需要申请内存空间,否则会发生段错误(你可以把stu2 malloc空间那行代码去掉自行验证)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Student
{
int age;
char name[256];
int ID;
};
void temp(struct Student *p,struct Student *m)
{
p->age = m->age;
// p->name = m->name;
strcpy(p->name,m->name);
p->ID =m->ID;
}
int main()
{
struct Student *stu1;
struct Student *stu2;
stu1 = (struct Student*)malloc(sizeof(struct Student));
stu2 = (struct Student*)malloc(sizeof(struct Student));
stu1->age = 20;
strcpy(stu1->name,"zhangsan");
stu1->ID = 123456;
temp(stu2,stu1);
//memcpy(stu2,stu1,sizeof(struct Student));
printf("stu2->age=%d,stu2->name=%s,stu2->ID=%d\n",stu2->age,stu2->name,stu2->ID);
memset(stu2,0,sizeof(struct Student));
printf("stu2->age=%d,stu2->name=%s,stu2->ID=%d\n",stu2->age,stu2->name,stu2->ID);
return 0;
}
3.当结构体变量stu1通过函数赋值给结构体变量是不需要申请空间的
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Student
{
int age;
char name[256];
int ID;
};
void temp(struct Student *p,struct Student *m)
{
p->age = m->age;
// p->name = m->name;
strcpy(p->name,m->name);
p->ID =m->ID;
}
int main()
{
struct Student stu1;
struct Student stu2;
stu1.age = 20;
strcpy(stu1.name,"zhangsan");
stu1.ID = 123456;
temp(&stu2,&stu1);
printf("stu2.age=%d,stu2.name=%s,stu2.ID=%d\n",stu2.age,stu2.name,stu2.ID);
memset(&stu2,0,sizeof(struct Student));
printf("stu2.age=%d,stu2.name=%s,stu2.ID=%d\n",stu2.age,stu2.name,stu2.ID);
return 0;
}
4.定义两个结构体变量stu1,stu2,将stu1的地址赋值给结构体指针stu3,通过子函数调用,也能成功赋值给stu2
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Student
{
int age;
char name[256];
int ID;
};
void temp(struct Student *p,struct Student *m)
{
p->age = m->age;
// p->name = m->name;
strcpy(p->name,m->name);
p->ID =m->ID;
}
int main()
{
struct Student stu1;
struct Student stu2;
stu1.age = 20;
strcpy(stu1.name,"zhangsan");
stu1.ID = 123456;
struct Student *stu3=&stu2;
temp(stu3,&stu1);
printf("stu2.age=%d,stu2.name=%s,stu2.ID=%d\n",stu2.age,stu2.name,stu2.ID);
memset(&stu2,0,sizeof(struct Student));
printf("stu2.age=%d,stu2.name=%s,stu2.ID=%d\n",stu2.age,stu2.name,stu2.ID);
return 0;
}
5.三个结构体指针stu1,stu2,stu3;将结构体指针stu1赋值给stu3,调用子函数,结构体指针stu2也能获取到stu1的值
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Student
{
int age;
char name[256];
int ID;
};
void temp(struct Student *p,struct Student *m)
{
p->age = m->age;
// p->name = m->name;
strcpy(p->name,m->name);
p->ID =m->ID;
}
int main()
{
struct Student *stu1;
struct Student *stu2;
struct Student *stu3;
stu1 = (struct Student*)malloc(sizeof(struct Student));
stu2 = (struct Student*)malloc(sizeof(struct Student));
stu1->age = 20;
strcpy(stu1->name,"zhangsan");
stu1->ID = 123456;
stu3=stu1;
temp(stu2,stu3);
//memcpy(stu2,stu1,sizeof(struct Student));
printf("stu2->age=%d,stu2->name=%s,stu2->ID=%d\n",stu2->age,stu2->name,stu2->ID);
memset(stu2,0,sizeof(struct Student));
printf("stu2->age=%d,stu2->name=%s,stu2->ID=%d\n",stu2->age,stu2->name,stu2->ID);
return 0;
}
所有结果都是如下所示: