Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagi-Rastar committed Sep 23, 2024
1 parent 8f12b36 commit fd577ff
Show file tree
Hide file tree
Showing 39 changed files with 4,296 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .obsidian/app.json
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
}
38 changes: 12 additions & 26 deletions .obsidian/workspace.json

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions docs/_CS/240905-C语言语法特性.md
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 标准中一个强大的特性,它允许开发者编写更灵活和可重用的代码。然而,它的使用可能会使代码的阅读和理解变得更加困难,因此应该在确实需要时才使用。
5 changes: 0 additions & 5 deletions docs/_CS/240905-语法特性.md

This file was deleted.

46 changes: 46 additions & 0 deletions docs/_CS/240923-C语言自编小模块笔记.md
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 结构体,
9 changes: 9 additions & 0 deletions docs/_Embedded/240923-STM32 杂项知识点.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
tags:
- 八股
- 秋招
- stm32
- flash
---
# 240923-STM32 杂项知识点

11 changes: 9 additions & 2 deletions docs/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
- '2024':
- '2024-09-23':
- newpage:
text: ' [ Embedded ] > STM32 杂项知识点'
href: /sagi_database/_Embedded/240923-STM32 杂项知识点
- newpage:
text: ' [ CS ] > C语言自编小模块笔记'
href: /sagi_database/_CS/240923-C语言自编小模块笔记
- '2024-09-18':
- newpage:
text: ' [ CS ] > C语言八股'
Expand All @@ -8,8 +15,8 @@
text: ' [ Embedded ] > 更新storage以及mqtt模块部分,逐步实现私有json协议'
href: /sagi_database/_Embedded/240905-更新storage以及mqtt模块部分,逐步实现私有json协议
- newpage:
text: ' [ CS ] > 语法特性'
href: /sagi_database/_CS/240905-语法特性
text: ' [ CS ] > C语言语法特性'
href: /sagi_database/_CS/240905-C语言语法特性
- '2024-08-30':
- newpage:
text: ' [ Embedded ] > 缺陷检测光源控制器开发笔记'
Expand Down
50 changes: 48 additions & 2 deletions site/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@








Expand Down Expand Up @@ -420,11 +422,11 @@


<li class="md-nav__item">
<a href="/_CS/240905-%E8%AF%AD%E6%B3%95%E7%89%B9%E6%80%A7/" class="md-nav__link">
<a href="/_CS/240905-C%E8%AF%AD%E8%A8%80%E8%AF%AD%E6%B3%95%E7%89%B9%E6%80%A7/" class="md-nav__link">


<span class="md-ellipsis">
240905-语法特性
240905-C语言语法特性
</span>


Expand Down Expand Up @@ -455,6 +457,27 @@









<li class="md-nav__item">
<a href="/_CS/240923-C%E8%AF%AD%E8%A8%80%E8%87%AA%E7%BC%96%E5%B0%8F%E6%A8%A1%E5%9D%97%E7%AC%94%E8%AE%B0/" class="md-nav__link">


<span class="md-ellipsis">
240923-C语言自编小模块笔记
</span>


</a>
</li>




</ul>
</nav>

Expand Down Expand Up @@ -504,6 +527,8 @@








Expand Down Expand Up @@ -869,6 +894,27 @@



<li class="md-nav__item">
<a href="/_Embedded/240923-STM32%20%E6%9D%82%E9%A1%B9%E7%9F%A5%E8%AF%86%E7%82%B9/" class="md-nav__link">


<span class="md-ellipsis">
240923-STM32 杂项知识点
</span>


</a>
</li>










<li class="md-nav__item">
<a href="/_Embedded/ESP32%E9%A1%B9%E7%9B%AE%E6%B1%87%E6%80%BB/" class="md-nav__link">

Expand Down
50 changes: 48 additions & 2 deletions site/_CS/240716-内存管理/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@








Expand Down Expand Up @@ -448,11 +450,11 @@


<li class="md-nav__item">
<a href="../240905-%E8%AF%AD%E6%B3%95%E7%89%B9%E6%80%A7/" class="md-nav__link">
<a href="../240905-C%E8%AF%AD%E8%A8%80%E8%AF%AD%E6%B3%95%E7%89%B9%E6%80%A7/" class="md-nav__link">


<span class="md-ellipsis">
240905-语法特性
240905-C语言语法特性
</span>


Expand Down Expand Up @@ -483,6 +485,27 @@









<li class="md-nav__item">
<a href="../240923-C%E8%AF%AD%E8%A8%80%E8%87%AA%E7%BC%96%E5%B0%8F%E6%A8%A1%E5%9D%97%E7%AC%94%E8%AE%B0/" class="md-nav__link">


<span class="md-ellipsis">
240923-C语言自编小模块笔记
</span>


</a>
</li>




</ul>
</nav>

Expand Down Expand Up @@ -532,6 +555,8 @@








Expand Down Expand Up @@ -897,6 +922,27 @@



<li class="md-nav__item">
<a href="../../_Embedded/240923-STM32%20%E6%9D%82%E9%A1%B9%E7%9F%A5%E8%AF%86%E7%82%B9/" class="md-nav__link">


<span class="md-ellipsis">
240923-STM32 杂项知识点
</span>


</a>
</li>










<li class="md-nav__item">
<a href="../../_Embedded/ESP32%E9%A1%B9%E7%9B%AE%E6%B1%87%E6%80%BB/" class="md-nav__link">

Expand Down
Loading

0 comments on commit fd577ff

Please sign in to comment.