结构体
结构体(struct)是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构。
结构体的定义方式:
struct 结构体名{
结构体所包含的变量或数组
};
例如:
struct Student{ //声明一个结构体类型Student
int num;
char name[20];
char sex;
int age;
float score;
};
>结构体类型的创建
(1)先声明结构体类型再定义变量名
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
float score; //声明一个单精度型变量
};
Student student1, student2;// 定义结构体类型变量student1和student2
printf("%d\n", sizeof(Student));
system("pause");
return 0;
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
float score; //声明一个单精度型变量
}Student1,Student2;
Student student1, student2;// 定义结构体类型变量student1和student2
printf("%d\n", sizeof(Student));
printf("%d\n", sizeof(Student1));
printf("%d\n", sizeof(Student2));
system("pause
结构体(struct)是由一系列具有相同类型或不同类型的数据构成的数据集合,也叫结构。
结构体的定义方式:
struct 结构体名{
结构体所包含的变量或数组
};
例如:
struct Student{ //声明一个结构体类型Student
int num;
char name[20];
char sex;
int age;
float score;
};
>结构体类型的创建
(1)先声明结构体类型再定义变量名
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
float score; //声明一个单精度型变量
};
Student student1, student2;// 定义结构体类型变量student1和student2
printf("%d\n", sizeof(Student));
system("pause");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
struct Student{ //声明一个结构体类型Student
int num; //声明一个整形变量num
char name[20]; //声明一个字符型数组name
char sex; //声明一个字符型变量sex
int age; //声明一个整形变量age
float score; //声明一个单精度型变量
}Student1,Student2;
Student student1, student2;// 定义结构体类型变量student1和student2
printf("%d\n", sizeof(Student));
printf("%d\n", sizeof(Student1));
printf("%d\n", sizeof(Student2));
system("pause