1 概述
数组:描述一组具有相同类型数据的有序集合,用于处理大量相同类型的数据运算
有时我们需要将不同类型的数据组合成一个有机的整体,如:一个学生有学号/姓名/性别/年龄/地址等属性。显然单独定义以上变量比较繁琐,数据不便于管理
C语言中给出了另一种构造数据类型——结构体
2 结构体变量的定义和初始化
定义结构体变量的方式:
-
先声明结构体类型再定义变量名
-
在声明类型的同时定义变量
-
直接定义结构体类型变量(无类型名)
结构体类型和结构体变量关系:
- 结构体类型:指定了一个结构体类型,它相当于一个模型,但其中并无具体数据,系统对之也不分配实际内存单元
- 结构体变量:系统根据结构体类型(内部成员状况)为之分配空间
// 结构体类型的定义
struct stu
{
char name[50];
int age;
};
// 先定义类型,再定义变量(常用)
struct stu s1 = {"Taoy", 18};
// 定义类型同时定义变量
struct stu2
{
char name[50];
int age;
}s2 = {"Tao", 19};
struct
{
char name[50];
int age;
}s3 = {"Taooo", 21};
3 结构体成员的使用
#include <stdio.h>
#include <sring.h>
// 结构体类型的定义
struct stu
{
char name[50];
int age;
}
int main()
{
struct stu s1;
// 如果是普通变量,通过点运算符操作结构体成员
strcpy(s1.name, "abc");
s1.age = 18;
printf("s1.name = %s, s1.age = %d\n", s1.name, s1.age);
// 如果是指针变量,通过->操作结构体成员
strcpy((&s1)->name, "test");
(&s1)->age = 22;
printf("(&s1)->name = %s, (&s1)->age = %d\n", (&s1)->name, (&s1)->age);
return 0;
}
4 结构体数组
#include <stdio.h>
// 统计学生成绩
struct stu
{
int num;
char name[20];
char sex;
float score;
}
int main()
{
struct stu boy[5] = {
{ 101, "Li ping", 'M', 45 },
{ 102, "Zhang ping", 'M', 62.5 },
{ 103, "He fang", 'F', 92.5 },
{ 104, "Cheng ling", 'F', 87 },
{ 105, "Wang ming", 'M', 58 }};
int i = 0;
int c = 0;
float ave,s = 0;
for(i = 0; i < 5; i++)
{
s += boy[i].score; //计算总分
if(boy[i].score < 60)
{
c += 1; //统计不及格人的分数
}
}
printf("s = %f\n", s); // 打印总分数
ave = s / 5;
printf("average = %f\n count=%d\n\n", ave, c);
for(i = 0; i < 5; i++)
{
printf("name = %s, score = %f\n",boy[i].name, boy[i].score);
// printf(" name=%s, score=%f\n", (boy + i )->name, (boy + i)->score);
}
return 0;
}
5 结构体套结构体
#include <stdio.h>
struct person
{
char name[20];
char sex;
}
struct stu
{
int id;
struct person info;
}
int main()
{
struct stu s[2] = {1, "Lily", 'F', 2, "yuri", "M"};
int i = 0;
for(i = 0; i < 2; i++)
{
printf("id = %d\tinfo.name=%s\tinfo.sex=%c\n", s[i].id, s[i].info.name, s[i].info.sex);
}
return 0;
}
6 结构体赋值
#include <stdio.h>
#include <string.h>
struct stu
{
char name[50];
int age;
}
int main()
{
struct stu s1;
//如果是普通变量,通过点运算符操作结构体成员
strcpy(s1.name, "abc");
s1.age = 18;
printf("s1.name = %s, s1.age = %d\n", s1.name, s1.age);
//相同类型的两个结构体变量,可以相互赋值
//把s1成员变量的值拷贝给s2成员变量的内存
//s1和s2只是成员变量的值一样而已,它们还是没有关系的两个变量
struct stu s2 = s1;
//memcpy(&s2, &s1, sizeof(s1));
printf("s2.name = %s, s2.gae = %d\n", s2.name, s2.age);
return 0;
}
7 结构体和指针
7.1. 指向普通结构体变量的指针
#include <stdio.h>
// 结构类型的定义
struct stu
{
char name[50];
int age;
}
int main()
{
struct stu s1 = {"Tao", 18};
// 如果是指针变量,通过->操作结构体成员
struct stu *p = &s1;
printf("p->name = %s, p->age=%d\n", p->name, p->age);
printf("(*p).name = %s, (*p).age=%d\n", (*p).name, (*p).age);
return 0;
}
7.2. 堆区结构体变量
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct stu
{
char name[50];
int age;
}
int main()
{
struct stu *p = NULL;
p = (struct stu *)malloc(sizeof(struct stu));
// 如果是指针变量,通过->操作结构体成员
strcpy(p->name, "test");
p->age = 22;
printf("p->name = %s, p->age=%d\n", p->name, p->age);
printf("(*p).name = %s, (*p).age=%d\n", (*p).name, (*p).age);
free(p);
p = NULL;
return 0;
}
7.3. 结构体套一级指针
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 机构体类型的定义
struct stu
{
char *name; // 一级指针
int age;
};
int main()
{
struct stu *p = NULL;
p = (struct stu *)malloc(sizeof(struct stu));
p->name = malloc(strlen("test") + 1);
strcpy(p->name, "test");
p->age = 19;
printf("p->name = %s, p->age = %d\n", p->name, p->age);
printf("(*p).name = %s, (*p).age=%d\n", (*p).name, (*p).age);
if(p->name != NULL)
{
free(p->name);
p->name = NULL;
}
if(p != NULL)
{
free(p);
p = NULL;
}
return 0;
}
8 结构体做函数参数
8.1. 结构体普通变量做函数参数
#include <stdio.h>
#include <string.h>
// 结构类型的定义
struct stu
{
char name[50];
int age;
}
// 函数参数为结构体普通变量
void set_stu(struct stu tmp)
{
strcpy(tmp.name, "Tao");
tmp.age = 18;
printf("tmp.name = %s, tmp.age = %d\n", tmp.name, tmp.age);
}
int main()
{
struct stu s = {0};
set_stu(s); // 值传递
printf("s.name = %s, s.age = %d\n", s.name, s.age);
return 0;
}
8.2. 结构体指针变量做函数参数
#include <stdio.h>
#include <string.h>
// 结构体类型的定义
struct stu
{
char name[50];
int age;
};
void set_stu_pro(struct stu *tmp)
{
strcpy(tmp->name, "Tao");
tmp->age = 18;
}
int main()
{
struct stu s = {0};
set_stu_pro(&s); // 地址传递
printf("s.name = %s, s.age = %d\n", s.name, s.age);
return 0;
}
8.3. const修饰结构体指针形参变量
// 结构体类型的定义
struct stu
{
char name[50];
int age;
}
void fun1(struct stu * const p)
{
//p = NULL; // err
p->age = 18;
}
// void(struct stu const* p)
void(const struct stu * p)
{
p = NULL; // ok
// p->age = 10; //err
}
void fun3(const struct stu * const p)
{
//p = NULL; //err
//p->age = 10; //err
}