1. 为什么需要结构体?
`
#include <stdio.h>
#include <stdlib.h>
int main() {
int age;
float score;
char name[100];
int age2;
float score2;
char name2[100];
printf("\n这样的定义显然不行,满足不了要求.\n");
printf("Hello world!\n");
return 0;
}
为了表示一些复杂的事物,而普通的基本类型无法满足实际要求。
2.什么是结构体?
把一些基本类型数据组合在一起形成的一个新的复合数据类型。
3. 如何定义结构体?
3.1 第一种定义方式:
struct Student{
int age;
float score;
char sex;
};
三个基本类型int,float,char组合成了新的数据类型structStudent
3.2 第二种定义方式
`struct Student2{
int age;
float score;
char sex;
} st2;`
直接写变量的名字,这样不好,只能定义一次。
3.3 第三种定义方式
struct{
int age;
float score;
char sex;
} st3;
后两种定义方式都不好,不建议使用。
4. 怎么使用结构体变量?
4.1 赋值和初始化
代码说明
`
#include <stdio.h>
#include <stdlib.h>
//定义了一个新的数据类型,并没有定义变量
struct Student{
int age;
float score;
char sex;
};
int main() {
//struct Student st = {80, 66.6, 'F'};//定义的同时赋值
struct Student st2;
st2.age =10;
st2.score =88;
st2.sex = 'F';
printf("%d, %.2f, %c!\n",st2.age,st2.score,st2.sex);
return 0;
}
`
4.2 如何取出结构体变量中的每一个成员(重点)
i. 结构体变量名.成员名st2.age
ii. 指针变量名 ->成员名 struct Student * pst = &st;//&st不能写成 st。 pst->age = 80;//第二种取值的方式,多用第二种方式。
iii. pst->age= 80;在计算机内部会被转化成*(pst).age,这就是->的含义,一种运行规定。所以pst->age等价于(*pst).age,也等价于st.age 。
VI. pst->age的含义:pst所指向的那个结构体变量中的age这个成员。
4.3 结构体变量和结构体指针变量作为函数参数传递的问题
示例:发送地址还是发送内容
指针的优点之一:快速的传递数据,耗用内存小,执行速度快。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int age;
char sex;
char name[100];
};
//结构体的长度是多少,很复杂的问题
//不需要返回值
void InputStudent(struct Student * pstu)//发送的是地址
{
pstu->age = 10;
strcpy(pstu->name,"张三");
pstu->sex = 'M';
}
void OutputStudent(struct Student ss)//发送的是内容,因为不需要修改内容。
//printf("%d, %c, %s\n", ss->age, ss->sex,ss->name);//发送的是地址,占用内存小。
//Q:是发送地址好?还是发送内容好?
{
printf("%d, %c, %s\n", ss.age, ss.sex, ss.name);
}
int main(void)
{
struct Student st;
//st有108个地址,但是为了减小内存的耗费,也为了提高执行速度,推荐发送地址。
printf("%d\n",sizeof(st));
InputStudent(&st);//对结构体变量输入
//printf("%d, %c, %s\n", st.age, st.sex, st.name);
OutputStudent(st);//对结构体变量输出
printf("Hello world!\n");
return 0;
}
/*void InputStudent(struct student stu)//无法修改main函数之间st的值
{
stu.age = 10;
strcpy(stu.name, "张三");//不能写成stu.name = "张三";
stu.sex = 'F';
}*/
4.4 结构体变量的运算
需求: 动态构造一个数组,存放学生的信息,然后按分数排序输出。
补充:冒泡排序:
#include <stdio.h>
#include <stdlib.h>
void sort(int * a, int len){
inti, j, t;
for(i=0; i<len-1;++i) {
for (j =0; j< len-1-i; ++j){
if (a[j] >a[j+1])//>表示升序,<表示降序
{
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
}
int main(void){
int a[6] = {10, 2, 9, -8, 11, 0};
inti = 0;
sort(a,6);
for(i = 0; i<6;++i){
printf("%d ", a[i]);
}
printf("\n");
printf("Hello world!\n");
return 0;
}
注意事项:
struct Student{
int age;
char sex;
char name[100];
};//分号不能省
/*
-----------------------------------
Struct Student st1,st2;
st1+st2, st1*st2, st1/st2 都是错误的。
st1 = st2, 或者 st2 = st1 都是正确的
---------------------------------------
*/
需求实现:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct student
{
intage;
float score;
char name[100];
};
int main(void)
{
intlen;
inti,j;
struct student * pArr;
struct student t;
printf("please enter the number of total student:");
printf("len = ");
scanf("%d", &len);
pArr = (struct student *)malloc(len*sizeof(int));
//输入
for(i = 0; i<len; ++i)
{
printf("请输入第%d个学生的信息:", i+1);
printf("age = ");
scanf("%d", &pArr[i].age);
printf("name = ");
scanf("%s", pArr[i].name);
//这里不需要加&,因为name是数组名,本身就已经是数组首元素的地址
//所以pArr[i].name不能该成&pArr[i].name
printf("score = ");
scanf("%f", &pArr[i].score);
}
//排序
for(i = 0; i < len-1; ++i)
{
for (j = 0; j=len-1-i; ++j)
{
if (pArr[j].score >pArr[j+1].score)
{
t = pArr[j];//不能写成pArr[j].score,为什么?
//如果写成pArr[j].score,则只是成绩互换,其他信息没有换。
pArr[j] = pArr[j+1];
pArr[j+1] =t;
}
}
}
printf("\n\n学生的信息是:");
//输出
for(i = 0; i<len; ++i)
{
printf("第%d个学生的信息: ",i+1);
printf("age = %d\n ", pArr[i].age );
//scanf("%d", &pArr[i].age);
printf("name = %s\n", pArr[i].name);
//printf("%s", pArr[i].name)
//这里不需要加&,因为name是数组名,本身就已经是数组首元素的地址
//所以pArr[i].name不能该成&pArr[i].name
printf("score = %.2lf", pArr[i].score);
//scanf("%f", &pArr[i].age)
printf("\n");
}
printf("Hello world!\n");
return 0;
}