首页 新闻 会员 周边

为什么输出的a.name的值是b.name 的值?

0
悬赏园豆:10 [已解决问题] 解决于 2015-09-19 11:27

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct stu{
char *name,gender;
int score;
};
main(){
struct stu a={NULL,'m',290},b;
a.name=(char *) malloc(10);
strcpy (a.name,"zhao");

b=a;
b.gender='f';
b.score=350;
strcpy(b.name,"qian");

printf("%s,%c,%d ",a.name,a.gender,a.score);
printf("%s,%c,%d",b.name,b.gender,b.score);
}

pureheart的主页 pureheart | 初学一级 | 园豆:132
提问于:2015-09-19 10:59
< >
分享
最佳答案
0

因为你进行的是内存拷贝,两种方式可看出差别:

1.值拷贝

2.在strcpy(b.name,"qian");前重新malloc

收获园豆:10
jello chen | 大侠五级 |园豆:7336 | 2015-09-19 11:23

懂了,谢谢

pureheart | 园豆:132 (初学一级) | 2015-09-19 11:26
其他回答(1)
0

你不自己断点看看到哪一步哪个值发生变化,光别人讲给你听是没用的,要学会自己找问题,学会程序调试很重要

夜空下的男子 | 园豆:31 (初学一级) | 2015-09-19 11:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册