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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值