优化了命名
This commit is contained in:
232
C08-构造类型/C8-构造类型.md
Normal file
232
C08-构造类型/C8-构造类型.md
Normal file
@@ -0,0 +1,232 @@
|
||||
# 目录
|
||||
- [目录](#目录)
|
||||
- [构造类型](#构造类型)
|
||||
- [结构体](#结构体)
|
||||
- [产生的原因及意义](#产生的原因及意义)
|
||||
- [类型的描述](#类型的描述)
|
||||
- [嵌套定义](#嵌套定义)
|
||||
- [定义变量(变量,数组,指针),初始化及成员引用](#定义变量变量数组指针初始化及成员引用)
|
||||
- [结构体占用的内存空间大小](#结构体占用的内存空间大小)
|
||||
- [函数传参(值,地址)](#函数传参值地址)
|
||||
- [共用体](#共用体)
|
||||
- [产生及意义](#产生及意义)
|
||||
- [类型描述](#类型描述)
|
||||
- [嵌套定义](#嵌套定义-1)
|
||||
- [定义变量(变量,数组,指针),初始化及成员引用](#定义变量变量数组指针初始化及成员引用-1)
|
||||
- [占用内存大小](#占用内存大小)
|
||||
- [函数传参(值,地址)](#函数传参值地址-1)
|
||||
- [位域](#位域)
|
||||
- [枚举](#枚举)
|
||||
- [typedef](#typedef)
|
||||
|
||||
|
||||
# 构造类型
|
||||
|
||||
## 结构体
|
||||
|
||||
### 产生的原因及意义
|
||||
|
||||
存放不同类型的数据到一起。
|
||||
|
||||
### 类型的描述
|
||||
|
||||
```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;
|
||||
};
|
||||
```
|
||||
|
||||
## typedef
|
||||
|
||||
为已有的数据类型改名。
|
||||
|
||||
`typedef 已有的数据类型 新名字;`
|
||||
36
C08-构造类型/enum.c
Normal file
36
C08-构造类型/enum.c
Normal 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
C08-构造类型/struct1.c
Normal file
113
C08-构造类型/struct1.c
Normal 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);
|
||||
}
|
||||
57
C08-构造类型/typedef.c
Normal file
57
C08-构造类型/typedef.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// #define INT int;
|
||||
|
||||
typedef int INT;
|
||||
|
||||
/**
|
||||
* #define IP int *;
|
||||
* IP p,q; -> int *p, q;
|
||||
*
|
||||
* typedef int *IP;
|
||||
* IP p,q; -> int *p, *q;
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* typedef int ARR[6]; --> int [6] -> ARR
|
||||
*
|
||||
* ARR a; -> int a[6];
|
||||
*
|
||||
*/
|
||||
|
||||
#if 0
|
||||
typedef struct node_st
|
||||
{
|
||||
int i;
|
||||
float p;
|
||||
} NODE, *NODEP;
|
||||
|
||||
NODE a; // struct node_st a;
|
||||
NODE *p;
|
||||
|
||||
NODEP p; // struct node_st *p;
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* typedef int FUNC(int); --> int(int) FUNC;
|
||||
* FUNC f; --> int f(int);
|
||||
*
|
||||
* typedef int *FUNCP(int);
|
||||
* FUNCP p; --> int *p(int);
|
||||
*
|
||||
* typedef int *(*FUNCP)(int);
|
||||
* FUNCP p; --> int *(*p)(int);
|
||||
*
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
INT i = 100; // int i;
|
||||
|
||||
printf("%d\n", i);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user