1.从堆区申请能存5个结构体变量的数组的空间,完成数组中成员的输入,根据学生成绩,用选择排序的方式,对学生排序并输出。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct stu
{
char student[100];
float score;
}stu;
void input(stu p[],int n);
void paixu(stu p[],int n);
void output(stu p[],int n);
int main(int argc, const char *argv[])
{
int sumstu=5;
stu *p=(stu*)malloc(sizeof(struct stu)*sumstu);
input(p,sumstu);
paixu(p,sumstu);
output(p,sumstu);
return 0;
}
void input(stu p[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("请输入第%d个学生姓名:",i+1);
scanf("%s",p[i].student);
printf("请输入第%d个学生成绩:",i+1);
scanf("%f",&p[i].score);
}
}
void paixu(stu p[],int n)
{
int i;
int j;
for(i=0;i<n;i++)
{
int m=i;
for(j=i;j<n;j++)
{
if(p[m].score<p[j].score)
{
m=j;
}
}
stu temp=p[i];
p[i]=p[m];
p[m]=temp;
}
}
void output(stu p[],int n)
{
for(int i=0;i<n;i++)
{
printf("第%d名是%s,他的成绩是%.1f\n",i+1,p[i].student,p[i].score);
}
}
2.
1.结构体中的每一个成员变量的存储位置(相对于结构体中第一个成员的偏移量),必须在其对齐量的整数倍上
2.结构体整体的大小,必须是最大对齐成员的对齐量的整数
3.成员对齐量=成员本身的大小>操作系统对齐量?操作系统的对齐量:成员本身的大小
求以下结构体的大小
typedef struct
{
int id;
char name[50];
char grade[3];
} Student;
//60
typedef struct
{
int id;
char name[50];
Student student;
} Teacher;
//116
typedef struct
{
int id;
char name[50];
Teacher teacher;
} Course;
//172