枚举
结构体
![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604556128942-ab2280c2-2273-45c2-813d-8255c451b39f.png#align=left&display=inline&height=132&margin=%5Bobject%20Object%5D&name=image.png&originHeight=264&originWidth=804&size=131426&status=done&style=none&width=402)
结构声明的形式
三种定义方式
结构成员
结构运算
结构指针
代码
#include <stdio.h>
#include <stdlib.h>
struct date
{
int month;
int day;
int year;
};
struct date today;
int main()
{
struct date today;
today = (struct date){07,31,2014};
struct date day;
struct date *pDate = &today;
day = today;
day.year = 2015;
printf("Today'day is %i-%i-%i.\n",today.year,today.month,today.day);
printf("The day' date is %i-%i-%i.\n",day.year,day.month,day.day);
printf("address of today is %p\n",pDate);
return 0;
}
结构作为函数参数
输入结构
通过函数读入结构体参数,并将其赋值给结构体
- 因为函数是传值的,所以无法改变传入结构体的值
解决方案一:定义无输入值的getStruct()函数,直接返回结构体
解决方案二:通过结构体指针将结构体带入函数
#include <stdio.h>
#include <stdlib.h>
//定义结构体
struct point
{
int x;
int y;
};
//定义 结构体 指针函数 or 指针函数
struct point* getStruct(struct point*);
//输出函数,其参数为结构体的值
void outPut(struct point);
//打印函数,其参数为结构体的指针
void print(const struct point *p);
int main()
{
struct point y = {0,0};
getStruct(&y);
outPut(y);
outPut(*getStruct(&y));
print(getStruct(&y));
return 0;
}
struct point* getStruct(struct point *p)
{
scanf("%d",&p->x);
scanf("%d",&p->y);
printf("%d,%d\n",p->x,p->y);
return p;
}
void outPut(struct point p)
{
printf("%d,%d",p.x,p.y);
}
void print(const struct point *p)
{
printf("%d %d\n",p->x,p->y);
}
结构数组
结构中的结构
嵌套的结构
![image.png](https://cdn.nlark.com/yuque/0/2020/png/807843/1604563563155-03310fd2-cefb-4989-8344-4aeb01e03c3a.png#align=left&display=inline&height=344&margin=%5Bobject%20Object%5D&name=image.png&originHeight=688&originWidth=1040&size=507049&status=done&style=none&width=520)
typedef
- Data 替换了中间的所有的东西
联合
得到一个整数的各个字节
union的用处