c语言第七章习题

  • 格式:doc
  • 大小:42.50 KB
  • 文档页数:5

1 第七章 结构体

一、选择题

1、若有语句:char s1[]="string",s2[8],*s3,*s4="string2";则对库函数strcpy的错误调用是()

A)strcpy(s1,"string2"); B)strcpy(s4,"string1"); C)strcpy(s3,"string1"); D)strcpy(s1,s2);

2、下面程序的运行结果是()

fun(int **a,int p[2][3])

{**a=p[1][1];}

main()

{int x[2][3]={2,4,6,8,10,12},*p;p=(int *)malloc(sizeof(int));fun(&p,x);printf("%d\n",*p);}

A)10 B)12 C)6 D)8

3、以下scanf函数调用语句中对结构体变量成员的不正确引用是()。

struct pupil

{ char name[20]; int age; int sex;} pup[5],*p; p=pup;

A)scanf("%s",pup[0].name); B)scanf("%d",&pup[0].age);

C)scanf("%d",&(p->sex)); D)scanf("%d",p->age);

4、以下对结构体变量stul中成员age的非法引用是()。

struc student

{ int age; int num; }stul,*P; p=&stul;

A)stul.age B)student.age C)p->age D)(*p).age

5、设有以下说明和定义语句,则下面表达式中值为3的是()。

struct s

{ int i1; struct s *i2;};

struct s a[3]={1,&a[1],2,&a[2],3,&a[0]},*ptr;

ptr=&a[1];

A)ptr->i1++ B)ptr++->i1 C)*ptr->i1 D)++ptr->i1

6、若要利用下面的程序片段使指针变量p指向一个存储整型变量的存储单元,则《1》中应填入的内容是:

int *p; p=《1》malloc(sizeof(int));

A)int B)int * C)(*int) D)(int *)

7、以下对C语言中共用体类型数据的叙述正确的是()。

A)可以对共有体变量名直接赋值 B)一个共用体变量中可以同时存放其所有成员

C)一个共有体变量中不能同时存放其所有成员

D)共用体类型定义中不能出现结构体类型的成员

8、当说明一个共用体变量时系统分配给它的内存是()。 2 A)各成员所需内存量的总和 B)结构中第一个成员所需内存量

C)成员中占内存量最大者所需的容量 D)结构中最后一个成员所需内存量

9、下面对typedef的叙述中不正确的是()。

A)用typedef可以定义各种类型名,但不能用来定义变量

B)用typedef可以增加新类型

C)用typedef只是将已存在的类型用一个新的标识符来代表

D)用typedef有利于程序的通用和移植

10、有以下定义和语句,则以下引用形式不合法的是()。

struct s

{ int i1; struct s *i2,*i0;};

struct s a[3]={2,&a[1],'\0',4,&a[2],&a[0],6,'\0',a[1]},*ptr; ptr=a;

A)ptr->i1++ B)*ptr->i2 C)++ptr->i0 D)*ptr->i1

11、设有如下定义:

struct sk

{ int n; float x;}data,*p;若要使p指向data中的n域,正确的赋值语句是()。

A)p=&data.n; B)*p=data.n;

C)p=(struct sk *)&data.n;

D)p=(struct sk *)data.n;

12、若有以下程序段:

struct dent

{ int n; int *m; };

int a=1,b=2,c=3;

struct dent s[3]={{101,&a},{102,&b},{103,&c}};

main()

{ struct dent *p; p=s; ...}

则以下表达式中值为2的是()。

A)(p++)->m B)*(p++)->m C)(*p).m D)*(++p)->m

13、若有以下说明和语句,则对pup中sex域的正确引用方式是()。

struct pupil

{ char name[20]; int sex; }pup,*p; p=&pup;

A)p.pup.sex B)p->pup.sex C)(*p).pup.sex D)(*p).sex

14、设有以下语句:

struct st

{ int n; struct st *next; };

struct st a[3]={5,&a[1],7,&a[2],9,'\0'},*p; p=&a[0]; 则以下表达式的值为6的是()。 3 A)p++->n B)p->n++ C)(*p).n++ D)++p->n

15、以下程序的输出结果是()。

struct stu

{ int x; int *y; }*p;

int dt[4]={10,20,30,40};

struct stu a[4]={50,&dt[0],60,&dt[1],70,&dt[2],80,&dt[3]};

main()

{ p=a; printf("%d",++p->x); printf("%d",(++p)->x); printf("%d\n",++(*p->y));}

A)50,20,20 B)50,20,21 C)51,60,21 D)51,60,31

二、编程题

1、建立一个链表,每个结点包含的成员为:职工号、工资。用malloc函数开辟新结点。要求链表包含5个结点,从键盘输入结点中的数据。然后把这些结点的数据打印出来。要求用函数creat来建立函数,用list函数来输出数据。这5个职工的号码为101,103,105,107,109。用insert函数来新增一个职工的数据,这个新结点,按职工号顺序插入。用delete函数来删除一个结点。

附录:答案

一、选择题

1—5 CADBD 6—10 DCDCD

11—15 CDDDC

二、编程题

Struct work

{long num;

float gz;

};

创建

struct work * Create(struct work * head)

{

struct work * this, * new;

new=(struct work *) malloc(sizeof(struct stru));

if (head= =NULL)

head =new;

else

{

this=head;

while (this->next!=NULL) this=this->next;

this->next=new; 4 new->next=null

scanf(“%ld”,&new->num);

scanf(“%f”.,&gz);

}

endif

return(head);

}

浏览

void list ( struct work * head)

{ struct work * this, * new;

if (head==null)

printf(“no data!”);

else

{

this=head;

do{

printf(“%ld,%f\n”,this->num,this->gz);

this=this->next;

}while(this!=NULL);

endif

}

}

插入

struct work *insert( struct work * head ,long x,float y )

{ struct work * this ,*new,* fro;

new=(struct work*) malloc( sizeof(struct work));

new->num=x; new->gz=y;

if (head= = NULL)

head=new;

else

{

this=head;

while((this!=NULL) &&(this->num>x))

{fro=this;

this=this->next; 5 }

if(this= =head)

{head=new;

new->next=this

}

else

{fro->next=new;

new->next=this;

}

endif

}

删除

struc work * del(struct work *head, int x)

{struct work *head, * this ,*fro;

this=head;

while((this!=NULL) &&(this->num!=x)

{fro=this;

thid=this- next;

}

if (this!=NULL)

If(this= =head)

{head=head->next;

free(this);

}

else

{

fro->next=this->next;

free(this);

}

endif

endif