若有以下程序 struct stu {char*name,gender;int score;}; main(){struct stu a={NULL,’m’,290),b; a.name=(char*)malloc(10);

admin2018-01-11  26

问题 若有以下程序
    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\n",b.name,b.gender,b.score);}
    则程序的输出结果是

选项 A、Zhao,m,290,Qian,f,350
B、Qian,f,350,Qian,f,350
C、Zhao,m,290,Zhao,e350
D、Qian,m,290,Qian,f,350

答案D

解析 主函数中定义结构体类型struct stu,有三个成员,第一个成员为字符指针变量。主函数中为stu类型变量a赋值,阿先为a.name成员分配存储空间,存入数据。b=a;语句执行完毕以后,a和b的各个成员值相同,需要注意的是b.name和a.name成员都保存了同一段内存的地址,因此当为变量b赋值的时候,同样更改了a的name成员,而其他成员由于有单独的存储空间而不会发生改变。
转载请注明原文地址:https://kaotiyun.com/show/NLxp777K
0

最新回复(0)