第八章完结
This commit is contained in:
226
Chapter8/C8-构造类型.md
Normal file
226
Chapter8/C8-构造类型.md
Normal file
@@ -0,0 +1,226 @@
|
||||
# 目录
|
||||
- [目录](#目录)
|
||||
- [构造类型](#构造类型)
|
||||
- [结构体](#结构体)
|
||||
- [产生的原因及意义](#产生的原因及意义)
|
||||
- [类型的描述](#类型的描述)
|
||||
- [嵌套定义](#嵌套定义)
|
||||
- [定义变量(变量,数组,指针),初始化及成员引用](#定义变量变量数组指针初始化及成员引用)
|
||||
- [结构体占用的内存空间大小](#结构体占用的内存空间大小)
|
||||
- [函数传参(值,地址)](#函数传参值地址)
|
||||
- [共用体](#共用体)
|
||||
- [产生及意义](#产生及意义)
|
||||
- [类型描述](#类型描述)
|
||||
- [嵌套定义](#嵌套定义-1)
|
||||
- [定义变量(变量,数组,指针),初始化及成员引用](#定义变量变量数组指针初始化及成员引用-1)
|
||||
- [占用内存大小](#占用内存大小)
|
||||
- [函数传参(值,地址)](#函数传参值地址-1)
|
||||
- [位域](#位域)
|
||||
- [枚举](#枚举)
|
||||
|
||||
|
||||
# 构造类型
|
||||
|
||||
## 结构体
|
||||
|
||||
### 产生的原因及意义
|
||||
|
||||
存放不同类型的数据到一起。
|
||||
|
||||
### 类型的描述
|
||||
|
||||
```c
|
||||
struct 结构体名
|
||||
{
|
||||
数据类型 成员1;
|
||||
数据类型 成员2;
|
||||
......
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 嵌套定义
|
||||
|
||||
### 定义变量(变量,数组,指针),初始化及成员引用
|
||||
|
||||
|
||||
|
||||
成员引用: 变量名. 成员名,指针->成员名,(*指针).成员名
|
||||
|
||||
|
||||
|
||||
### 结构体占用的内存空间大小
|
||||
|
||||
|
||||
|
||||
### 函数传参(值,地址)
|
||||
|
||||
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NAMESIZE 32
|
||||
|
||||
struct
|
||||
{
|
||||
int i;
|
||||
char ch;
|
||||
float f;
|
||||
} a =
|
||||
{
|
||||
1,
|
||||
'a',
|
||||
},
|
||||
b, c, *p, *q;
|
||||
|
||||
struct simp_st
|
||||
{
|
||||
int i;
|
||||
char ch;
|
||||
float f;
|
||||
// char ch1;
|
||||
}; // __attribute__((packed));
|
||||
|
||||
|
||||
struct student_st
|
||||
{
|
||||
int id;
|
||||
char name[NAMESIZE];
|
||||
// struct birthday_st birth;
|
||||
struct birthday_st
|
||||
{
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
} birth;
|
||||
int math;
|
||||
int chinese;
|
||||
};
|
||||
|
||||
|
||||
void func(struct simp_st *b)
|
||||
{
|
||||
printf("%d\n", sizeof(b));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// TYPE NAME = VALUE;
|
||||
|
||||
|
||||
struct simp_st a;
|
||||
struct simp_st *p = &a;
|
||||
|
||||
// func(a);
|
||||
|
||||
func(p);
|
||||
|
||||
|
||||
|
||||
|
||||
// printf("sizeof(point) = %d\n", sizeof(p));
|
||||
// printf("sizeof(struct) = %d\n", sizeof(a));
|
||||
// out:
|
||||
// sizeof(point) = 8
|
||||
// sizeof(struct) = 12
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
struct student_st stu = {10011, "Alan", {2011, 11, 11}, 98, 97};
|
||||
struct student_st *p = &stu;
|
||||
struct student_st arr[2] = {{.name = "Alan"}, {.name = "John"}};
|
||||
|
||||
p = &arr[0];
|
||||
|
||||
// struct student_st stu = {.math = 98, .chinese = 97};
|
||||
|
||||
// printf("%d %s %d-%d-%d %d %d\n",
|
||||
// stu.id,
|
||||
// stu.name,
|
||||
// stu.birth.year,
|
||||
// stu.birth.month,
|
||||
// stu.birth.day,
|
||||
// stu.math,
|
||||
// stu.chinese);
|
||||
|
||||
// printf("%d %s %d-%d-%d %d %d\n",
|
||||
// p->id,
|
||||
// p->name,
|
||||
// p->birth.year,
|
||||
// p->birth.month,
|
||||
// p->birth.day,
|
||||
// p->math,
|
||||
// p->chinese);
|
||||
|
||||
for (int i = 0; i < 2; i++, p++)
|
||||
{
|
||||
printf("%s ", p->name);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
struct simp_st a = {123, 456.789, 'a'};
|
||||
a.i = 112233;
|
||||
printf("%d %f %c", a.i, a.f, a.ch);
|
||||
#endif
|
||||
|
||||
exit(0);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
> 跳过
|
||||
|
||||
## 共用体
|
||||
|
||||
### 产生及意义
|
||||
|
||||
### 类型描述
|
||||
|
||||
```c
|
||||
union 共用体名
|
||||
{
|
||||
数据类型 成员名1;
|
||||
数据类型 成员名2;
|
||||
......
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 嵌套定义
|
||||
|
||||
### 定义变量(变量,数组,指针),初始化及成员引用
|
||||
|
||||
### 占用内存大小
|
||||
|
||||
### 函数传参(值,地址)
|
||||
|
||||
### 位域
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 枚举
|
||||
|
||||
```c
|
||||
enum 标识符
|
||||
{
|
||||
成员1;
|
||||
成员2;
|
||||
成员3;
|
||||
};
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user