9 结构体与共用体
(B)p->a
9.2 若有以下说明和语句,则表达式
(C)++p->a
(D)(p++)->a
中的值为 31。
struct wc
{int a;
int *b;
} *p;
int x0[ ]={11,12},x1[ ]={31,32};
stratic struct wc x[2]={100,x0,300,x1};
double c;
};
printf("%d\n", sizeof (struct st));
9.16 若有以下的说明和语句,则其输出结果为
。
union un
{ int i;
double y;
};
struct st
{ char a[10];
union un b;
};
printf("%d\n",sizeof (struct st)); 9.17 若有以下的说明和语句,则其输出结果为
printf("%d,%d,%d,%d\n", qiaut, cubs, pick, dodger);
}
9.19 下面程序的输出结果为
。
union abc
{ char c[2];
int a;
}un;
main()
{ un.a=16961;
printf("%d,%c\n",un.c[0],un.c[0]);
gets(sp[i].num);
}
}
getsort(USER *sp)
{ int i,j,k;
USER temp;
for(i=0;i<N-1;i++)
{ k=i;
for(j=i+1;j<N;j++)
if(strcmp(sp[k].name,sp[j].name)>0) k=j;
temp=sp[k]; sp[k]=sp[i]; sp[i]=temp;
{ USER sp[N],temp;
getdata(sp);
getsort(sp);
outdata(sp);
}
getdata(USER *sp)
{ int i;
printf("Enter %d name & phone number:\n",N);
for(i=0;i<N;i++)
{ gets(sp[i].name);
printf("\n the linklist is:"); p=head; if(head!=NULL) do
{ printf("%d", (1) ); p= (2) ; }while( (3) );
} main() {struct stud *head; ... print(head); ... } 9.10 已知 head 指向单链表的第一个结点,下列函数 del()将从单链表中删除值为 num 的第一个结点,请填空。 #include<stdio.h> struct student {int info; struct student *link; }; struct student *del(struct student *head, int num) { struct student *p1,*p2;
struct st
{ int a;
int b[2];
}a; (A)结构变量 a 和结构成员 a 同名,不合法 (B)程序运行时将为结构 st 分配 6 个字节内存单元 (C)程序运行时不为结构 st 分配内存单元
1
(D)程序运行时将为结构变量 a 分配 6 个字节内存单元 9.6 若有以下的说明:
static struct st a[3]={5,&a[1],7,&a[2],9,'\0',},*p;
p=&a[0];
则表达式
的值是 6。
(A)p++->n
(B)p->n++
(C)(*p).n++
(D)++p->n
9.5 若有以下的说明,已知 int 类型的变量占两个字节,则
的叙述是正
确的(多项选择)。
(B)*++(++p)->b (D)(p++)->b
二、填空题
9.8 下面程序实现对两个数 x,y 的判定,若 0≤x≤y≤100 条件成立,则计算 x2+y2, 并将结果输出,否则打印相应的出错信息并继续读数,直至输入正确。请填空。
enum ErrorData{Right, Lesson, Great100,MinMaxErr}; char *ErrorMessage[]={
2
if(max<min)return MinMaxErr; if(max>100)return Great100; if(min<0)return Lesson;
(3) ; } 9.9 已知 head 指向单链表的第一个结点,以下程序调用函数 print()输出这一单向链表, 请在程序中填空。 #INCLUDE<STDIO.H> #include<stdlib.h> struct stud { int info; struct stud *next; }; void print(struct stud *head) { struct stud *p;
"Enter Data Right", "Data<0 Error", "Data>100 Error", "x>y Error" }; main() { int status,x,y; do{
printf("Please Enter two number:(x,y)"); scanf("%d,%d", &x, &y); status= (1) ; printf("%s\n",ErrorMessage[ (2) ]); }while (status!=Right); printf("Result=%d",x*x+y*y); getch(); } int error(int min, int max) {
。
static struct
{char ch;
int i;
double x;, {'b',2,7.98},{'c',3,1.93}};
9.15 若有以下的说明和语句,已知 int 类型占两个字节,则其输出结果为
。
struct st
{char a[10];
int b;
}
6
} outdata(USER *sp) { int i;
printf("after sorted:\n"); for(i=0;i<N;i++) printf("%s,%s\n",sp[i].name,sp[i].num); }
7
4
do{
(2)
} (1) ;
return(n);
}
9.13 若有以下说明,则变量 w 在内存中所占的字节数是
。
union aa
{float x,y;
char c[6];
};
struct st {union aa v; float w[5]; double ave;}w;
9.14 若有以下的说明,则对初值中整数 2 的引用方式为
(D)p->name
使指针 p->b 的值增 1(多项选择)。
struct wc
{ int a;
int *b;
}*p;
int x0[ ]={11,16},x1[ ]={31,36};
static struct wc x[2]={100,x0,300,x1};
p=x; (A)*++p->b (C)*p->b++
else printf("%d not been found! \n",num); } return(head); } 9.11 下面函数 creat()用来建立一个带头结点的单向链表,新产生的结点总是插在链表 的末尾。单向链表的头指针作为函数值返回,请在程序中填空。 #include<stdio.h> struct list { char data; struct list *next; }; struct list *creat() { struct list *h, *p, *q; char ch; h= (1) malloc(sizeof(struct list)); p=q=h; ch=getchar(); while(ch!='?') { p= (2) malloc(sizeof(struct list)); p->data=ch; q->next=p; q=p; ch=getchar(); } p->next='\0'; (3) ; } 9.12 函数 count(head)统计链表结点的个数,head 为头指针,阅读程序并填空。 #include<stdio.h> struct student {char data; struct student *next; }; count(head) struct student * head; { int n=0; struct student * p=head;