结构体是一种构造类型数据,他与数组的区别是,数组中所有元素的数据类型是相同的,而结构体中的各个成员的数据类型可以不同。
可以看成结构体是由不同类型变量组成的一个集合。
结构体的定义
示例:
struct Student//自定义类型
{
int score;//成绩
char name[128];//名字
};
这里我们需要知道的是:
- 结构体在定义时不分配存储空间。
- 结构体变量的声明需要在主函数之上或者主函数中声明。
- 结构体右括号后边要加分号。
结构体变量的定义和使用
#include <stdio.h>
#include <stdlib.h>
struct Student//自定义类型
{
int score;//成绩
char name[128];//名字
};
int main()
{
//结构体变量的定义
//类型 变量名 初始值
struct Student s1 = {98,"刘冰"};
//结构体变量的使用
printf("结构体;score = %d \n",s1.score);
printf("结构体;name = %s \n",s1.name);
system("pause");
return 0;
}