结构体习题
一、选择题
1.有以下程序输出结果是( )。
#include
struct stu
{ int num;
char name[10];
int age; };
void fun(struct stu *p)
{ print("%s\n",(*p).name); }
main()
{ struct stu students[3]={{9801,"zhang",20},{9802,"Wang",19},{9803,"zhao",18}};
fun(students+2); }
A.Zhang
B.Zhao
C.Wang
D.18
2.设有如下定义:
struct sk
{ int a;
float b;} data,*p;
若有p=&data;则对data中的a 域的正确引用是( )。
A.(*p).data.a
B.(*p).a
C.p->data.a
D.p.data.a
3.根据下面的定义,能打出字母M的语句是( )。
struct person
{ char name[9];
int age;};
struct person class[10]={ 〞John〞,17, 〞Paul〞,19, 〞Mary〞,18, 〞Adam 〞,16};
A.printf(〞%c\n〞,class[3].name);
B.printf(〞%c\n〞,class[3].name[1]);
C.printf(〞%c\n〞,class[2].name[1]);
D.printf(〞%c\n〞,class[2].name[0]);
4. 下列程序的执行结果为( )。
#include
struct s1
{ char *s;
int i;
struct s1 *sip; };
main()
{ static struct s1 a[]={{"abcd",1,a+1},{"efgh",2,a+2},{"ijkl",3,a}};
struct s1 *p=a;
int i=0;
print("%s%s%s",a[0].s,p->s,a[2].sip->s);
print("%d%d",i+2,--a[i].i);
printf("%c\n",++a[i].s[3]); }
A.abcd abcd abcd 2 0 d
B.abcd efgh ijkl 2 0 d
C.abcd abcd ijkl 2 0 d
D.abcd abcd abcd 2 1 d
5. 下面程序的输出结果为( )
#include
struct st
{ int x;
int *y; } *p;
int dt[4]={10,20,30,40};
struct st aa[4]={50,&dt[0],60,&dt[1],70,&dt[2],80,&dt[3]};
main()
{ p=aa;
printf("%d\n",++p->x);
printf("%d\n",(++p)->x);
printf("%d\n",++(*p->y));
}
A.10
B.50
C. 51
D.60
20 60 60 70
20 21 21 31
6. 有以下程序的运行结果是( )。
#include
main()
{ struct STU
{ char name[9];
char sex;
double score[2]; };
struct STU a={“Zhao”,’m’,85.0,90.0},b={“Qian”,’f’,95.0,92.0};
b=a;
print(“%s,%c,%2.0f,%2.0f\n”,https://www.doczj.com/doc/8218676239.html,,b.sex,b.score[0],b.score[1]);}
A. Qian, f, 95, 92
B. Qian, m, 85, 90
C. Zhao, f, 95, 92
D. Zhao, m, 85, 90
7. 下面结构体的定义语句中,错误的是()。
A. struct ord
B. struct ord
{ {
int x; int x ;
int y; int y ;
int z; int z ;
}; }
struct ord a; struct ord a;
C. struct ord
D. struct
{ {
int x; int x ;
int y; int y ;
int z; int z ;
} a; }a;
8. 有以下程序运行后的输出结果是()。
#include
#include
struct A
{ int a;
char b[10];
double c; };
struct A f(struct A t);
main()
{ struct A a={1001,"ZhangDa",1098.0};
a=f(a);
print("%d,%s,%6.1f\n",a.a,a.b,a.c); }
struct A f(struct A t)
{ t.a=1002;
strcpy(t.b,"ChangRong");
t.c=1202.0;
return t; }
A. 1001,ZhangDa,1098.0
B.1002,ZhangDa,1202.0
C. 1001,ChangRong,1098.0
D.1002,ChangRong,1202.0 9.下面程序的运行结果是()。
#include
struct STU
{ char name[10];
int num; };
void f(char *name, int num)
{ struct STU s[2]={{"Sun",2044},{"Li",2045}};
num=s[0].num;
strcpy(name, s[0].name); }
main()
{ struct STU s[2]={{"Yang",2041},{"Guo",2042}},*p;
p=&s[1];
f(p->name, p->num);
printf("%s %d\n", p->name, p->num); }
A. Sun 2042
B. Sun 2044
C. Guo 2042
D. Yang 2041
10. 下面程序的运行结果是()。
#include
#include
struct STU
{ int num;
float score; };
void f(struct STU p)
{ struct STU s[2]={{2044,550},{2045,537}};
p.num = s[1].num;
p.score = s[1].score; }
main()
{ struct STU s[2]={{2041,703},{2042,580}};
f(s[0]);
printf("%d %3.0f\n", s[0].num, s[0].score); }
A. 2045 537
B. 2044 550
C. 2042 580
D. 2041 703
11. 有以下程序其运行结果是()。
#include
struct st
{ int x, y; }data[2]={1,10,2,20};
main()
{ struct st *p=data;
print("%d,",p->y);
printf("%d\n",(++p)->x); }
A.10,1
B. 20,1
C. 10,2
D. 20,2
12. 有以程序的运行结果是( )。
#include
#include
typedef struct
{ char name[9];
char sex;
float score[2];
} STU;
void f( STU a)
{ STU b={“Zhao” ,’m’,85.0,90.0} ; int i;
strcpy(https://www.doczj.com/doc/8218676239.html,,https://www.doczj.com/doc/8218676239.html,); a.sex=b.sex;
for(i=0;i<2;i++)
a.score[i]=
b.score[i]; }
main()
{ STU c={“Qian”,’f’,95.0,92.0};
f(c);
print(“%s,%c,%2.0f,%2.0f\n”,https://www.doczj.com/doc/8218676239.html,,c.sex,c.score[0],c.score[1]); }
A. Qian,f,95,92
B. Qian,m,85,90
C. Zhao,f,95,92
D. Zhao,m,85,90
13. 下面程序的运行结果是()。
#include
struct ord
{ int x,y; } dt[2]={1,2,3,4};
main()
{ struct ord *p=dt;
printf("%d,", ++p->x);
printf("%d\n", ++p->y); }
A. 1,2
B. 2,3
C. 3,4
D.4,1
14.以下结构体类型说明和变量定义中正确的是()。
A)typedef struct B)struct REC;
{int n; char c;}REC; {int n; char c;};
REC t1,t2; REC t1,t2;
C)typedef struct REC ; D)struct
{int n=0; char c=’A’;}t1,t2;{int n;char c;}REC t1,t2;
二、填空题
1.结构体是不同数据类型的数据集合,作为数据类型,必须先说明结构体【】,再说明结构体变量。
2. 有以下说明定义语句,可用a.day引用结构体成员day,请写出引用结构体成员a.day的其它两种形式
【】、【】。
struct d
{ int day;
char mouth;
int year;} a,*b;
b=&a;
3. 若已有类型定义:
struct date
{ int year;
int month;
int day;};
写出下面程序的运行结果【】。
void change(struct date d)
{ d.year=2008;
d.month=d.day=8;}
main()
{ struct date date1={0,0,0};
change(date1);
print("%d,%d,%d\n",date1.year,date1.month,date1.day);}
4. 若已有类型定义:
struct date
{ int year;
int month;
int day;};
写出下面程序的运行结果【】。
void change(struct date *p)
{ (*p).year=2008;
(*p).month=(*p).day=8;}
main()
{ struct date date1={0,0,0};
change(&date1);
print("%d,%d,%d\n",date1.year,date1.month,date1.day);}
5. 分析以下程序的输出结果是【】。
#include
#include
struct STUD
{ int no;
char *name;
int score;
};
main()
{ struct STUD st1={1,"Mary",85},st2;
st2.no=2;
https://www.doczj.com/doc/8218676239.html,=(char *)malloc(sizeof(10));
strcpy(https://www.doczj.com/doc/8218676239.html,,"Smith");
st2.score=78;
print("%s\n",(st1.score>st2.score? https://www.doczj.com/doc/8218676239.html,: https://www.doczj.com/doc/8218676239.html,)); }
6. 以下程序中函数fun的功能是:统计person所指结构体数组中所有性别(sex)为M的记录的个数,存入变量n中,并做为函数值返回。请填空:
#include
#define N 3
typedef struct
{
int num;
char nam[10];
char sex;
}SS;
int fun(SS person[])
{ int i,n=0;
for (i=0;i if(【】=='M' ) n++; return n; } main() { SS W[N]={{1,"AA",'F'},{2,"BB",'M'},{3,"CC",'M'}}; int n; n=fun(W); printf("n=%d\n",n); } 7. 有如下定义: struct { int x; int y; }s[2]={{1,2},{3,4}},*p=s; 则表达式++p->x的结果是【】。 8. 人员的记录由编号(id)和出生年月日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号是唯一。下面函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数输出;若指定编号不存在,返回编号为空串的数据。 #define N 20 typedef struct student { char id[40]; int year; int month; int date; } STU; 【】fun( STU std[], char *id) { int i; STU a={" ", 9999, 99,99}; for(i=0;i if(strcmp(std[i].id, id) ==0) return std[i]; return a; } main() { STU s[N], ps; char id[40]; int i; print("输入所有人员的编号和出生日期:"); for(i=0;i scanf("%s%d,%d,%d", s[i].id, &s[i].year, &s[i].month,&s[i].date); print("输入待查找人员的编号:"); scanf("%s",id); ps=fun(s, id); if(strcmp(ps.id, " ")!=0) print("查找结果:%s, %d, %d, %d", ps.id, ps.year, ps.month,ps.date); else print("返回空串,不存在该编号"); } 9. 下面程序通过定义学生结构体变量,存储了学生的学号、姓名和3门的成绩。函数fun的功能是将形参a所指结构体变量中的数据赋给函数中的结构体变量b,并修改了b中的学号和姓名,最后输出修改后的数据。例如:a所指变量中的学号、姓名和三门课的成绩依次是:10001、“zhangsan"、95、80、88,则修改后的输出b中的数据应为:10002、“lisa”、95、80、88。 #include #include struct student { int num; char name[40]; float score[3]; }; void fun(struct student a) { struct student b; int i; b=a; b.num=10002; strcpy(【】,"lisa"); print("改变后的信息:"); print("%d,%s",b.num, https://www.doczj.com/doc/8218676239.html,); for(i=0;i<3;i++) print("%f", b.score[i]); } main() {struct student aa={10001,"zhangsan",95,80,88}; fun(aa); } 10. 将形参std所指结构体数组中年龄最大者的数据作为函数值返回。 typedef struct stu { int num; char name[40]; int age; } STD; STD fun( STD s[], int n) { STD max; int i; max=s[0]; for(i=1;i if(max.age<【】) max=s[i]; return max; } main() { STD ss[5], max; int i; for(i=0;i<5;i++) scanf("%d%s%d",&ss[i].num,ss[i].name,&ss[i].age); max=fun(ss,5); print("年龄最大者的数据信息:%d,%s,%d",max.num,https://www.doczj.com/doc/8218676239.html,,max.age); } 11. 下面程序实现的是:将形参指针所指结构体数组中的三个元素按num成员进行升序排列。将程序补充完整。 typedef struct s { int num; char name[40]; float score; } PERSON; void fun(PERSON std[3]) { PERSON temp; if(std[0].num>std[1].num) { temp=std[0]; std[0]=std[1]; std[1]=temp; } if(std[0].num>std[2].num) { temp=std[0]; std[0]=std[2]; std[2]=temp; } if(std[1].num>std[2].num) { temp=std[1]; std[1]=std[2]; std[2]=temp; } } main() { PERSON std[]={ {12, "sunny", 89}, {8,"train", 73}, { 21,"smelle", 91} }; int i; fun(【】); for(i=0;i<3;i++) print("%d,%s,%f\n", std[i].num, std[i].name, std[i].score); } 12. 下面程序的输出结果是【】。 struct ks { int a; int *b; }; main() { struct ks s[4],*p; int n=1,i; for(i=0;i<4;i++) { s[i].a=n; s[i].b=&s[i].a; n=n+2; } p=&s[0]; printf("%d,%d\n",++(*p->b),*(s+2)->b); } 13. 下面程序的运行结果是【】。 #include struct NODE { int num; struct NODE *next; }; main() { struct NODE s[3]={{1, '\0'},{2, '\0'},{3, '\0'}}, *p, *q, *r; int sum=0; s[0].next=s+1; s[1].next=s+2; s[2].next=s; p=s; q=p->next; r=q->next; sum+=q->next->num; sum+=r->next->next->num; printf("%d\n",sum); } 14. 设有定义: struct person { int ID; char name[12]; }p; 请将scanf(“%d”, 【】);语句补充完整,使其能够为结构体变量p的成员ID正确读入数据。 15. 有以下程序运行结果是【】 #include typedef struct { int num; double s; }REC; void fun1( REC x ) { x.num=23; x.s=88.5; } main() { REC a={16,90.0 }; fun1(a); printf("%d\n",a.num); } 16. 下列程序的运行结果为:【】。 #include #include struct A { int a; char b[10]; double c; }; void f(struct A *t); main() { struct A a={1001, "ZhangDa", 1098.0}; f(&a); print("%d,%s,%6.1f\n", a.a, a.b, a.c); } void f(struct A *t) { strcpy(t->b, "ChangRong"); } 选择题:BBDAC DBDAD CABA 填空题:1、类型2、(*b).day 、b->day 3、0,0,0 4、2008,8,8 5、Mary 6、person[i].sex 7、2 8、STU 9、https://www.doczj.com/doc/8218676239.html, 10、s[i].age 11、std 12、2,5 13、5 14、&p->ID 15、16 16、1001, ChangRong, 1098.0