以下程序拟读取一个姓氏,查找其对应的年龄并输出: #include<stdio.h> #include<string.h> #include<stdlib.h> struct person{char name[10];int age;}; struet p

admin2021-07-09  20

问题 以下程序拟读取一个姓氏,查找其对应的年龄并输出:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct person{char name[10];int age;};
struet person group[6]={"zhang",18,
"wang",20,
"li",19,
"zhao",18,
"liu".19
},*p;
main()
{int i,flag=0;
p=(struet person*)malloc(sizeof(struet person));
gets(&p->name);
for(i=0;i<6;i++)
if((stremp(p->name,group.name)==0))
{printf("%d\n",group.age);
flag=1;
}
if(flag==0)printf("No find!\n");
}
程序有语法错误,出错的是(  )。

选项 A、gets(&p->name);
B、p=(struet person*)malloc(sizeof(struet person))
C、if((stremp(p->name,group.name)==0))
D、printf("%d\n",group.age)

答案A

解析 语句gets(&p->name)中,gets的参数应该是成员变量name数组的首地址,而&p->name是首先获取数组name的首地址,然后再对首地址取地址符,所以这是错误的,正确参数应该是:gets(p->name);,故答案选A。
转载请注明原文地址:https://kaotiyun.com/show/j4tp777K
0

最新回复(0)