# C语言结构体

497人阅读 评论(0)

1、struct 的嵌套结构体
#include<stdio.h>
struct date
{
int month;
int day;
int year;
};
struct student
{
char *name;
int age;
char sex;
struct date birthday;
float score;
}boy1,*boy2,boy3={"wjy",22,'W',11,13,1991,80};
main(){
boy1.name = "yzmin";
boy1.age = 22;
boy1.sex = 'M';
boy1.score = 80;
boy1.birthday.year = 1991;
boy1.birthday.month = 5;
boy1.birthday.day = 10;
printf("Name = %s\nAge = %d\nSex = %c\nScore = %2.1f\nBirthday = %d-%d-%d\n",boy1.name,boy1.age,boy1.sex,boy1.score,boy1.birthday.year,boy1.birthday.month,boy1.birthday.day);
boy2 = &boy1;
printf("Name = %s\nAge = %d\nSex = %c\nScore = %2.1f\nBirthday = %d-%d-%d\n",boy2->name,boy2->age,boy2->sex,boy2->score,boy2->birthday.year,boy2->birthday.month,boy2->birthday.day);
printf("Name = %s\nAge = %d\nSex = %c\nScore = %2.1f\nBirthday = %d-%d-%d\n",boy3.name,boy3.age,boy3.sex,boy3.score,boy3.birthday.year,boy3.birthday.month,boy3.birthday.day);
}

2、结构体的数组方式

#define NUM 3
struct mem
{
char name[20];
char phone[10];
};
main()
{
struct mem man[NUM];
int i;
for(i=0;i<NUM;i++)
{
printf("input name:\n");
gets(man[i].name);
printf("input phone:\n");
gets(man[i].phone);
}
printf("name\t\t\tphone\n\n");
for(i=0;i<NUM;i++)
printf("%s\t\t\t%s\n",man[i].name,man[i].phone);
}

3、计算学生的平均成绩和不及格的人数。

struct stu
{
int num;
char *name;
char sex;
float score;
}boy[5]={
{101,"Li ping",'M',45},
{102,"Zhang ping",'M',62.5},
{103,"He fang",'F',92.5},
{104,"Cheng ling",'F',87},
{105,"Wang ming",'M',58},
};
main()
{
int i,c=0;
float ave,s=0;
for(i=0;i<5;i++)
{
s+=boy[i].score;
if(boy[i].score<60) c+=1;
}
printf("s=%.2f\n",s);
ave=s/5;
printf("average=%.2f\ncount=%d\n",ave,c);
}

4、结构体的指针

struct stu
{
int num;
char *name;
char sex;
float score;
} boy1={102,"Zhang ping",'M',78.5},*pstu;
main()
{
pstu=&boy1;
printf("Number=%d\nName=%s\n",boy1.num,boy1.name);
printf("Sex=%c\nScore=%f\n\n",boy1.sex,boy1.score);
printf("Number=%d\nName=%s\n",(*pstu).num,(*pstu).name);
printf("Sex=%c\nScore=%f\n\n",(*pstu).sex,(*pstu).score);
printf("Number=%d\nName=%s\n",pstu->num,pstu->name);
printf("Sex=%c\nScore=%f\n\n",pstu->sex,pstu->score);
}

本例程序定义了一个结构stu，定义了stu类型结构变量boy1并作了初始化赋值，还定义了一个指向stu类型结构的指针变量pstu。在main函数中，pstu被赋予boy1的地址，因此pstu指向boy1。然后在printf语句内用三种形式输出boy1的各个成员值。从运行结果可以看出：
结构变量.成员名
(*结构指针变量).成员名
结构指针变量->成员名

5、用指针变量输出结构数组

struct stu
{
int num;
char *name;
char sex;
float score;
}boy[5]={
{101,"Zhou ping",'M',45},
{102,"Zhang ping",'M',62.5},
{103,"Liou fang",'F',92.5},
{104,"Cheng ling",'F',87},
{105,"Wang ming",'M',58},
};
main()
{
struct stu *ps;
printf("No\tName\t\t\tSex\tScore\t\n");
for(ps=boy;ps<boy+5;ps++)
printf("%d\t%s\t\t%c\t%f\t\n",ps->num,ps->name,ps->sex,ps->score);
}

ps=&boy[1].sex;

ps=boy;(赋予数组首地址)

ps=&boy[0];(赋予0号元素首地址)

6、计算一组学生的平均成绩和不及格人数。用结构指针变量作函数参数编程。

struct stu
{
int num;
char *name;
char sex;
float score;}boy[5]={
{101,"Li ping",'M',45},
{102,"Zhang ping",'M',62.5},
{103,"He fang",'F',92.5},
{104,"Cheng ling",'F',87},
{105,"Wang ming",'M',58},
};
main()
{
struct stu *ps;
void ave(struct stu *ps);
ps=boy;
ave(ps);
}
void ave(struct stu *ps)
{
int c=0,i;
float ave,s=0;
for(i=0;i<5;i++,ps++)
{
s+=ps->score;
if(ps->score<60) c+=1;
}
printf("s=%f\n",s);
ave=s/5;
printf("average=%f\ncount=%d\n",ave,c);
}

7、分配一块区域，输入一个学生数据。

main()
{
struct stu
{
int num;
char *name;
char sex;
float score;
}  *ps;
ps=(struct stu*)malloc(sizeof(struct stu));
ps->num=102;
ps->name="Zhang ping";
ps->sex='M';
ps->score=62.5;
printf("Number=%d\nName=%s\n",ps->num,ps->name);
printf("Sex=%c\nScore=%f\n",ps->sex,ps->score);
free(ps);
ps = NULL;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：116567次
• 积分：1677
• 等级：
• 排名：千里之外
• 原创：25篇
• 转载：176篇
• 译文：0篇
• 评论：3条
文章分类
评论排行
最新评论