第八章完结

This commit is contained in:
lzy
2024-04-12 19:07:28 +08:00
parent 6d54ee810a
commit 387681af20
3 changed files with 375 additions and 0 deletions

226
Chapter8/C8-构造类型.md Normal file
View 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;
};
```

36
Chapter8/enum.c Normal file
View File

@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdlib.h>
enum day
{
MON = 1,
TUS,
THR,
WES,
FRI,
SAT,
SUN
};
enum
{
STATE_RUNNING = 1,
STATE_CANCELED,
STATE_OVER
};
// struct job_st
// {
// int id;
// int state;
// time_t start, end;
// };
int main()
{
// enum day a = MON;
exit(0);
}

113
Chapter8/struct1.c Normal file
View File

@@ -0,0 +1,113 @@
#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);
}