C语言基础11——结构体1
结构体1
1. 结构体
1.1 定义
用户自定义的数据类型,在结构体中可以包含若干不同数据类型(也可以相同)的成员变量,使这些数据项组合起来反应某一个信息
1.2 格式
struct 结构体名 (用户自定义的数据类型)
{
数据类型 成员变量1;
数据类型 成员变量2;
数据类型 成员变量3;
...
};
1.3 结构体变量
1.3.1 概念
通过结构体类型定义的变量
1.3.2 格式
struct 结构体名 变量名;
1.3.3 先定义结构体类型,再结构体变量
struct 结构体名
{
成员变量;
};
struct 结构体名 变量名;
#include <stdio.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu;return 0;
}
1.3.4 定义结构体的同时,定义结构体变量
#include <stdio.h>struct student
{char name[32];int id;int score;int age;
} stu;int main()
{return 0;
}
1.3.5 缺省结构体名定义结构体变量
#include <stdio.h>struct
{char name[32];int id;int score;int age;
} stu;int main()
{return 0;
}
1.4 赋值
1.4.1 定义变量的同时直接大括号赋值 (初始化)
注意:此时给结构体变量赋值的时候,要按照定义变量的顺序进行赋值,否则会导致数据错误或者赋值数据类型错误。
#include <stdio.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu = {"zhangsan", 12138, 80, 18};return 0;
}
1.4.2 定义变量时未初始化,然后对成员变量单独赋值
对变量单独赋值,需要对定义的结构体变量用变量名找到结构体内部定义的变量进行赋值,格式为:结构体名.变量名,此时的赋值可以不再按照定义时候的顺序赋值。
#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu;strcpy(stu.name, "lisi");stu.id = 12139;stu.score = 88;stu.age = 20;return 0;
}
1.4.3 点等法赋值
#include <stdio.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu = {.name = "wangwu",.id = 12140,.score = 0,.age = 20};return 0;
}
1.5 访问
通过 .访问:结构体变量名.成员变量名
scanf("%s %d %d %d", stu.name, &stu.id, &stu.score, &stu.age);
printf("%s %d %d %d\n", stu.name, stu.id, stu.score, stu.age);
练习1:创建一个名为student的结构体,包含姓名,学号,班级,分数,(数据类型自己定义),从终端输入学生的信息并打印。(参考代码见本文最后)
1.6 重定义 typedef
typedef int int _num; //int == int_num
int a; == int_num a;
typedef int * int _p; //int * == int_p
int *p = &a; //== int _p p = &a;
1.6.1 定义结构体的同时重定义
typedef struct student
{
int id;
int score;
} STU; // STU是结构体类型重定义的名字
struct student stu; == STU stu;
1.6.2 先定义结构体,然后重定义
struct student
{
int id;
int age;
};
typedef struct student STU;
2. 结构体数组
2.1 概念
结构体类型相同的变量组成的数组
2.2 格式
2.2.1 定义结构体的同时定义结构体数组
#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
}stu[3];int main()
{return 0;
}
2.2.2 先定义结构体,然后定义结构体数组
#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu[3];return 0;
}
2.3 赋值
2.3.1 定义结构体数组的同时赋值
#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu[3] = {{"zhangsan", 12138, 80, 18}, //第一个人的信息{"lisi", 12139, 92, 20}, //第二个人的信息{"wangwu", 12140, 0, 21} //第三个人的信息};return 0;
}
2.3.2 先定义结构体数组。再对结构体数组的每一个人元素分别赋值
与普通数组赋值方式相比就是多了个内部变量访问,也是需要通过数字下标找到第几个人
#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main()
{struct student stu[3];strcpy(stu[0].name, "zhangsan");stu[0].id = 12138;strcpy(stu[1].name, "lisi");stu[1].id = 12139;return 0;
}
2.4 结构体数组的输入输出:(for循环遍历)
#include <stdio.h>
#include <string.h>struct student
{char name[32];int id;int score;int age;
};int main(int argc, char const *argv[])
{struct student stu[3];for(int i = 0; i < 3; i++){scanf("%d %d %d %s",&stu[i].id, &stu[i].score, &stu[i].age, &stu[i].name);printf("%d %d %d %s\n",stu[i].id, stu[i].score, stu[i].age, stu[i].name);}return 0;
}
------------------------------------------------------这是一条分割线------------------------------------------------------
天才预设的上一篇跳转路径:C语言基础10——函数-CSDN博客
天才预设的下一篇跳转路径:未完待续~
天才预设的合集传送路径:C基础_Gu_shiwww的博客-CSDN博客
参考答案
练习1:
创建一个名为student的结构体,包含姓名,学号,班级,分数,(数据类型自己定义),从终端输入学生的信息并打印。
#include <stdio.h>typedef struct student
{int id;char name[10];int sorce;
}STU;void swap(STU *a,STU *b)
{STU c;c = *a;*a = *b;*b = c ;
}void sort1(STU a[],int m)
{for(int i = 0 ; i < m - 1 ; i++){for(int j = 0 ; j < m - 1 ; j ++){if(a[j].sorce > a[j + 1].sorce){swap(&a[j], &a[j + 1]);}}}
}int main()
{STU stu[5];for(int i = 0 ; i < 5 ; i ++){scanf("%d %s %d", &stu[i].id, stu[i].name, &stu[i].sorce);}sort1(stu,5);for(int i = 0 ; i < 5 ; i ++){printf("%d %s %d\n", stu[i].id, stu[i].name, stu[i].sorce);}return 0;
}