1、结构体内的变量和指针
结构体内是变量
struct School
{
char s_name[20];
int s_age;
};
void Init_School(struct School *sx)
{
char name[20] = "ligong";
strcpy_s(sx->s_name, 20, name);
sx->s_age = 90;
}
void Print_School(struct School *sx)
{
printf("name:%s\n", sx->s_name);
printf("age:%d\n", sx->s_age);
}
int main()
{
struct School sx;
Init_School(&sx);
Print_School(&sx);
return 0;
}
结构体内是指针
char name[20] = {
"ligong" };
struct School
{
char *s_name;
int s_age;
};
void Init_School(struct School *sp)
{
//static char name[20] = { "tulun" };//静态关键字static,使得变量name的生存期延长,
sp->s_name = name;
sp->s_age = 90;
}
void Print_School(struct School* sp)
{
printf("name:%s\n", sp->s_name);
printf("age:%d\n", sp->s_age);
}
int main()
{
struct School sx;
Init_School(&sx);
Print_School(&sx);
return 0;
}
2、学生结构体
1.编写函数打印学生信息
2.计算全班同学的总成绩、平均成绩和140分以下的人数。
3.按成绩升序排序,成绩相同按年龄降序排序。
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
struct Student
{
char s_name[20];
int age;
float score;
};
//打印学生信息
void Print_Cla(struct Student* cla, int n)
{
for (int i = 0; i < n; ++i)
{
printf