#include <stdio.h>
//创建结构体的第一种方式,推荐用这种
struct Student1{
int age;
float score;
char sex;
};
//创建结构体的第二种方式
struct Student2{
int age;
float score;
char sex;
}stu2;
//创建结构体的第三种方式
struct {
int age;
float score;
char sex;
}stu3;
void print(Student1 *stu){
printf("%d %f %c\n",stu->age,stu->score,stu->sex);
}
void print(Student2 *stu){
printf("%d %f %c\n",stu->age,stu->score,stu->sex);
}
void main(){
struct Student1 stu1={12,12.2,'n'};
print(&stu1);
struct Student2 *st=&stu2;
st->age=22;
st->score=22.2;
st->sex='n';
//st->age 相当于 (*st).age
print(st);
stu3.age=33;
stu3.score=33.3;
stu3.sex='v';
}
创建结构体的三种方式及其使用
最新推荐文章于 2024-06-30 11:15:46 发布