注:转载请标明原文出处链接:https://xiongyiming.blog.csdn.net/article/details/105565374
把一些基本类型数据组合在一起形成的一个新的复合数据类型称之为结构体。
为什么需要结构体?
在实际问题中,一组数据往往具有不同的数据类型。例如,在学生登记表中,姓名应为字符型;学号可为整型或字符型;年龄应为整型;性别应为字符型;成绩可为整型或实型。显然不能用一个数组来存放这一组数据。因为数组中各元素的类型和长度都必须一致,以便于编译系统处理。为了解决这个问题,C语言中给出了另一种构造数据类型——“结构(structure)”或叫“结构体”。 它相当于其它高级语言中的记录。 “结构”是一种构造类型,它是由若干“成员”组成的。每一个成员可以是一个基本数据类型或者又是一个构造类型。结构既是一种“构造”而成的数据类型,那么在说明和使用之前必须先定义它,也就是构造它。如同在说明和调用函数之前要先定义函数一样。
简单来说:结构体为了表示一些复杂的事物,而普通的基本类型无法满足实际需求。
方式1:最经常使用的方式
struct Student
{
int age;
float score;
char sex;
};
方式2
struct Student2
{
int age;
float score;
char sex;
} st2;
方式3
struct
{
int age;
float score;
char sex;
} st3;
代码示例
#include
#include
struct Student
{
int age;
float score;
char sex;
};
int main()
{
//初始化-方式1 定义同时赋值
struct Student st = { 80, 66.6, 'F' };
//初始化-方式2
struct Student st2; //定义
st2.age = 10; //赋值
st2.score = 88.8;
st2.sex = 'F';
printf("age=%d st.score=%.2f sex=%c\n", st.age, st.score, st.sex);
printf("age=%d st.score=%.2f sex=%c\n", st2.age, st2.score, st2.sex);
system("pause");
return 0;
}
运行结果
方式1
结构体变量名.成员名
方式2
指针变量->成员名
代码示例
#include
#include
struct Student
{
int age;
float score;
char sex;
};
int main()
{
struct Student st = { 80, 66.6, 'F' };
struct Student st2;
struct Student *pst = &st2; //定义指针变量
pst->age = 90;//方式1
st2.score = 88.8;//方式2
pst->sex = 'F';
printf("age=%d st.score=%.2f sex=%c\n", st.age, st.score, st.sex);
printf("age=%d st.score=%.2f sex=%c\n", pst->age, pst->score, pst->sex);
system("pause");
return 0;
}
运行结果
要求:通过指针和函数对结构体变量的输入和输出
代码示例
#pragma warning( disable : 4996)
#include
#include
#include
#include
struct Student
{
int age;
char sex;
char name[100];
};
void InputStudent(struct Student * pstu);//对结构体变量进行输入
void OutputStudent(struct Student * pstu);//对结构体变量进行输出
int main()
{
struct Student st;
InputStudent(&st);//对结构体变量进行输入
OutputStudent(&st);//对结构体变量进行输出
system("pause");
return 0;
}
void InputStudent(struct Student * pstu) //指针变量pstu只占用4个字节
{
pstu->age = 10;//(*pstu).age
strcpy(pstu->name, "zhangsan");////(*pstu).name
pstu->sex = 'F'; //(*pstu).sex
}
void OutputStudent(struct Student * pstu)
{
printf("age=%d name=%s sex=%c\n", (*pstu).age, (*pstu).name, (*pstu).sex);
}
/* 也可以使用
void OutputStudent(struct Student stu)
{
printf("age=%d name=%s sex=%c\n", stu.age, stu.name, stu.sex);
}
*/
运行结果
对结构体变量进行输入和输出时候,传地址还是传内容?
为了提高程序执行速度,推荐使用发送地址的方式。假设结构体占用内存,那么传输内容将耗费内存资源。
代码示例
#pragma warning( disable : 4996)
#include
#include
#include
#include
struct Student
{
int age;
char sex;
char name[10000];
};
void InputStudent(struct Student * pstu);//对结构体变量进行输入
void OutputStudent1(struct Student * pstu);//传地址
void OutputStudent2(struct Student stu); //传内容
int main()
{
struct Student st;
InputStudent(&st);//对结构体变量进行输入
OutputStudent1(&st);//传地址 对结构体变量进行输出
OutputStudent2(st); //传内容
system("pause");
return 0;
}
void InputStudent(struct Student * pstu) //指针变量pstu只占用4个字节
{
pstu->age = 10;//(*pstu).age
strcpy(pstu->name, "zhangsan");////(*pstu).name
pstu->sex = 'F'; //(*pstu).sex
}
void OutputStudent1(struct Student * pstu)
{
printf("传地址占用%d字节\n", sizeof(pstu));
printf("age=%d name=%s sex=%c\n", (*pstu).age, (*pstu).name, (*pstu).sex);
}
void OutputStudent2(struct Student stu)
{
printf("传内容占用%d字节\n",sizeof(stu));
printf("age=%d name=%s sex=%c\n", stu.age, stu.name, stu.sex);
}
运行结果
指针的优点
代码示例
#pragma warning( disable : 4996)
#include
#include
#include
#include
struct Student
{
int age;
char sex;
char name[100];
};
int main()
{
struct Student st1,st2;
st1.age = 18;
strcpy(st1.name, "lisi");
st1.sex = 'M';
st2 = st1;//结构体赋值
printf("age=%d name=%s sex=%c\n", st2.age, st2.name, st2.sex);
system("pause");
return 0;
}
运行结果
要求:由用户输入学生信息(年龄,姓名,成绩)并依次打印输出。
代码示例
#pragma warning( disable : 4996)
#include
#include
#include
#include
struct Student
{
int age;
char name[100];
float score;
};
int main()
{
struct Student st;
int len;
struct Student *pArr;
int i;
printf("请输入学生的个数:\n");
printf("len=");
scanf("%d", &len);
pArr = (struct Student *)malloc(len * sizeof(struct Student));
//输入学生信息
for (i = 0; i < len; i++)
{
printf("请输入第%d学生的信息:\n",i+1);
printf("age= ");
scanf("%d", &pArr[i].age);
printf("name= ");
scanf("%s", pArr[i].name);//name是数组名,代表数组首地址 所有不需要加&
printf("score= ");
scanf("%f", &pArr[i].score);
}
//输出学习信息
for (i = 0; i < len; i++)
{
printf("第%d学生的信息是:\n", i + 1);
printf("age=%d\n",pArr[i].age);
printf("name=%s\n", pArr[i].name);
printf("score=%f\n", pArr[i].score);
printf("\n");
}
//方式2 注意指针范围
/*
for (i = 0; i < len; pArr++,i++)
{
printf("第%d学生的信息是:\n", i + 1);
printf("age=%d\n", pArr->age);
printf("name=%s\n", pArr->name);
printf("score=%f\n", pArr->score);
printf("\n");
}
*/
system("pause");
return 0;
}
运行结果