-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f12b36
commit fd577ff
Showing
39 changed files
with
4,296 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"attachmentFolderPath": "./240918-C语言八股", | ||
"attachmentFolderPath": "./240905-更新storage以及mqtt模块部分,逐步实现私有json协议", | ||
"newLinkFormat": "relative", | ||
"useMarkdownLinks": true | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
# 240905-C语言语法特性 | ||
|
||
## 1 复合字面量 | ||
|
||
>复合字面量(Compound Literals)是 C99 标准引入的一个特性,它允许你在表达式中创建一个数组或结构体,并且可以立即使用。复合字面量的语法是:`(type){ initializer-list }`。 | ||
举例来说,如下之前新写的函数: | ||
|
||
```c | ||
debug_print_array("My Array", UINT8, (Data){.uint8=5}, UINT8_ARRAY, (Data){.uint8_array=(uint8_t[]){1, 2, 3, 4, 5}}, 0, 5); | ||
``` | ||
对于 `(Data){.uint8=5}` 以及 `(Data){.uint8_array=(uint8_t[]){1, 2, 3, 4, 5}}` 就使用了复合字面量的特性,在调用 `debug_print_array` 的形参表达式中创建了 `Data` 结构体,并立即初始化。 | ||
另外,复合字面量的生命周期和其所在的作用域保持一致。 | ||
>复合字面量(Compound Literals)这个名字来自于它的构成。在 C 语言中,"复合"(Compound)通常指的是由多个部分组成的数据类型,例如结构体(Structures)和数组(Arrays)。"字面量"(Literals)则是源代码中表示值的固定表示法,例如 `123` 是一个整数字面量,`"hello"` 是一个字符串字面量。 | ||
>因此,"复合字面量"就是一个由多个部分组成的字面量,它可以是一个结构体字面量,也可以是一个数组字面量。例如,`(struct {int x; int y;}){1, 2}` 是一个结构体字面量,`(int[]){1, 2, 3}` 是一个数组字面量。这些都是复合字面量的例子。 | ||
## 2 泛型选择器 | ||
在 C11 标准中,`_Generic()` 是一个关键字,它被引入作为一种类型安全的编译时选择机制。它允许程序员基于表达式的类型来选择不同的函数或代码块执行。这在处理多种数据类型时非常有用,可以减少代码的重复,并提高代码的可维护性。 | ||
### 2.1 语法 | ||
`_Generic()` 选择器的基本语法如下: | ||
```c | ||
_Generic((expr), type1: val1, type2: val2, ..., default: val_default) | ||
``` | ||
|
||
- `expr` 是要检查类型的表达式。 | ||
- `type1`, `type2`, ... 是 `expr` 可能的类型。 | ||
- `val1`, `val2`, ... 是当 `expr` 分别匹配 `type1`, `type2`, ... 时返回的值或执行的代码块。 | ||
- `default` 是当 `expr` 不匹配任何指定类型时的默认返回值或执行的代码块。 | ||
|
||
### 2.2 示例 | ||
|
||
假设我们有一个函数,它根据输入的类型返回不同的值: | ||
|
||
```c | ||
#include <stdio.h> | ||
|
||
#define SQUARE(x) _Generic((x), \ | ||
default: (x * x), \ | ||
int: (x * x), \ | ||
char: (x * x), \ | ||
float: (x * x), \ | ||
double: (x * x)) | ||
|
||
int main() { | ||
int i = 5; | ||
char c = 'a'; | ||
double d = 3.14; | ||
|
||
printf("Square of %d is %d\n", i, SQUARE(i)); | ||
printf("Square of '%c' is %d\n", c, SQUARE(c)); | ||
printf("Square of %f is %f\n", d, SQUARE(d)); | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
在这个例子中,`SQUARE` 宏使用 `_Generic()` 来根据参数的类型选择不同的代码块。由于所有类型都返回相同的代码块(x * x),所以这个例子并没有太多实际意义,但它展示了 `_Generic()` 的基本用法。 | ||
|
||
### 2.3 注意事项 | ||
|
||
1. **类型匹配**:`_Generic()` 会根据表达式的静态类型来选择分支,而不是运行时类型。 | ||
2. **默认分支**:必须提供一个 `default` 分支,以处理未明确列出的类型。 | ||
3. **类型安全**:`_Generic()` 选择器提供了类型安全的方式,因为它在编译时就确定了要执行的代码。 | ||
|
||
`_Generic()` 是 C11 标准中一个强大的特性,它允许开发者编写更灵活和可重用的代码。然而,它的使用可能会使代码的阅读和理解变得更加困难,因此应该在确实需要时才使用。 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
tags: | ||
- c语言 | ||
- 复用模块 | ||
--- | ||
# 240923-C语言自编小模块笔记 | ||
|
||
## 1 字符串自编协议 | ||
|
||
>对于字符串协议,一种更好的方法是使用一个映射表(map)或者查找表(lookup table),将字符串命令映射到对应的处理函数。这样可以避免使用多个 `strstr` 和 `if` 语句,使代码更加清晰和易于维护。 | ||
```c | ||
typedef void (*config_processing_func)(char*); | ||
|
||
typedef struct | ||
{ | ||
const char *command; | ||
config_processing_func func; | ||
} cmd_map; | ||
``` | ||
- 函数指针类型,名为 `config_processing_func` ,没有返回值,形参为字符串指针 | ||
- 结构体,包含需要匹配的 command & 处理函数 | ||
```c | ||
static const cmd_map screen_cmd_map[] = | ||
{ | ||
{"MC+WAN+WIFI=", wifi_config_processing}, | ||
{"MC+WAN+ETH=", eth_config_processing}, | ||
{"MC+TSUM=", tsum_config_processing}, | ||
{"MC+TNUM=", tnum_config_processing}, | ||
}; | ||
static const size_t cmd_map_size = sizeof(screen_cmd_map) / sizeof(screen_cmd_map[0]); | ||
for (size_t i = 0; i < cmd_map_size; i++) | ||
{ | ||
char *temp_ptr = strstr((char const *)temp_buffer_ptr, screen_cmd_map[i].command); | ||
if (temp_ptr != NULL) | ||
{ | ||
screen_cmd_map[i].func(temp_ptr); | ||
} | ||
} | ||
``` | ||
|
||
创建一个 cmd_map 类型的数组,其内部每一个成员都是一个 cmd_map 结构体, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
tags: | ||
- 八股 | ||
- 秋招 | ||
- stm32 | ||
- flash | ||
--- | ||
# 240923-STM32 杂项知识点 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.