/*
结构体变量定义有三种方法
1) 先定义结构体,然后在定义结构体变量
struct Student{
//学生学号
int sno;
//学生姓名
char name[21];
//学生年龄
int age;
// 学生成绩
float score;
};
注意:
1)结构体定义完成以后,计算机并不会给结构体分配内存空间
2)会在定义结构体变量后,分配存储空间
//结构体变量定义格式:
struct 结构体名 结构体变量名;
struct Student stu1; //这句话表示定义一个Student结构体类型的变量,变量名称是stu1;
//stu1 因为是Student 类型,stu1可以存放学生的学号、姓名、年龄、成绩
struct Student stu2,stu3,stu4; // 可以定义多个结构体变量 其中每个结构体变量都是可以存放学生的学号、姓名、年龄、成绩
2) 定义结构体的同时,定义结构体变量
格式:
struct 结构体名{
}结构体变量1,结构体变量2....;
struct Student{
//学生学号
int sno;
//学生姓名
char name[21];
//学生年龄
int age;
// 学生成绩
float score;
}stu5,stu6,stu7; //也是用Student结构体定义了三个结构体变量,名称分别为stu5,stu6,stu7
3) 使用匿名结构体定义结构体变量
struct {
}结构体变量1,结构体变量2....;
struct {
int *color;
int lunzi;
int speed;
}car1,car2;
*/
#include<stdio.h>
#include<stdlib.h>
struct Student{
//学生学号
int sno;
//学生姓名
char name[21];
//学生年龄
int age;
// 学生成绩
float score;
};
int main (){
struct Student stu1; //这句话表示定义了一个Student结构体类型的变量,变量名称是stu1;
// 第二种,定义结构体的同事,定义结构体变量
struct Student{
//学生学号
int sno;
//学生姓名
char name[21];
//学生年龄
int age;
// 学生成绩
float score;
}stu5,stu6,stu7; //也是用Student结构体定义了三个结构体变量,名称分别为stu5,stu6,stu7
//使用匿名结构体定义结构体变量
struct {
int *color;
int lunzi;
int speed;
}car1,car2;
getchar();
return 0;
}
结构体变量定义有三种方法
1) 先定义结构体,然后在定义结构体变量
struct Student{
//学生学号
int sno;
//学生姓名
char name[21];
//学生年龄
int age;
// 学生成绩
float score;
};
注意:
1)结构体定义完成以后,计算机并不会给结构体分配内存空间
2)会在定义结构体变量后,分配存储空间
//结构体变量定义格式:
struct 结构体名 结构体变量名;
struct Student stu1; //这句话表示定义一个Student结构体类型的变量,变量名称是stu1;
//stu1 因为是Student 类型,stu1可以存放学生的学号、姓名、年龄、成绩
struct Student stu2,stu3,stu4; // 可以定义多个结构体变量 其中每个结构体变量都是可以存放学生的学号、姓名、年龄、成绩
2) 定义结构体的同时,定义结构体变量
格式:
struct 结构体名{
}结构体变量1,结构体变量2....;
struct Student{
//学生学号
int sno;
//学生姓名
char name[21];
//学生年龄
int age;
// 学生成绩
float score;
}stu5,stu6,stu7; //也是用Student结构体定义了三个结构体变量,名称分别为stu5,stu6,stu7
3) 使用匿名结构体定义结构体变量
struct {
}结构体变量1,结构体变量2....;
struct {
int *color;
int lunzi;
int speed;
}car1,car2;
*/
#include<stdio.h>
#include<stdlib.h>
struct Student{
//学生学号
int sno;
//学生姓名
char name[21];
//学生年龄
int age;
// 学生成绩
float score;
};
int main (){
struct Student stu1; //这句话表示定义了一个Student结构体类型的变量,变量名称是stu1;
// 第二种,定义结构体的同事,定义结构体变量
struct Student{
//学生学号
int sno;
//学生姓名
char name[21];
//学生年龄
int age;
// 学生成绩
float score;
}stu5,stu6,stu7; //也是用Student结构体定义了三个结构体变量,名称分别为stu5,stu6,stu7
//使用匿名结构体定义结构体变量
struct {
int *color;
int lunzi;
int speed;
}car1,car2;
getchar();
return 0;
}