c语言共用体习题,2016C语言习题全集及答案:结构体和共用体02.doc

2016C语言习题全集及答案:结构体和共用体02

第八单元 结构体和共用体

读程序题,写出程序运行的结果。

1、

#include

struct abc { int a, b, c; };

main()

{ struct abc s[2]={{1,2,3},{4,5,6}};

int t;

t=s[0].a+s[1].b;

printf("%d \n",t);

}

2、

#include

struct stu

{ int num;char name[10]; int age;};

void fun(struct stu *p)

{ printf("%s\n" ,(*p).name); }

void main()

{ struct stu students[3]={{9801,"Zhang",20} ,{9802,"Wang", 19} ,

{9803,"Zhao",18} };

fun(students+2);

}

3、

#include

void main()

{ enum team { my ,your=4 ,his ,her=his+10};

printf("%d%d%d%d\n",my,your,his,her);

}

4、

#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] };

void main()

{ p=aa;

printf("%d, ", ++p->x);

printf("%d, ",(++p)->x);

printf("%d\n",++(*p->y));

}

5、

#include

union myun

{ struct

{ int x, y, z; } u;

int k;

} a;

void main()

{ a.u.x=4; a.u.y=5; a.u.z=6;

a.k=0;

printf("%d\n",a.u.x);

}

6、

#include

struct STU

{ char num[10]; float score[3]; };

void main()

{ struct STU s[3]={{“20021”,90,95,85},{“20022”,95,80,75},

{“20023”,100,95,90}},*p=s;

int i; float sum=0;

for(i=0;i<3;i++)

sum=sum+p->score[i];

printf(“%6.2f\n”,sum);

}

7、

#include

#include

struct NODE

{ int num; struct NODE *next; };

void main()

{ struct NODE *p,*q,*r;

p=(struct NODE*)malloc(sizeof(struct NODE));

q=(struct NODE*)malloc(sizeof(struct NODE));

r=(struct NODE*)malloc(sizeof(struct NODE));

p->num=10; q->num=20; r->num=30;

p->next=q;q->next=r;

printf(“%d\n”,p->num+q->next->num);

}

8、

#include

typedef union student

{ char name[10];

long sno;

char sex;

float score[4];

} STU;

void main()

{ STU a[5];

printf(“%d\n”,sizeof(a));

}

2

你可能感兴趣的:(c语言共用体习题)