Skip to content

Commit 328804b

Browse files
review: fix compile errors & polish vol1 c_tutorials 07B/08B answers
1 parent 11afb67 commit 328804b

2 files changed

Lines changed: 34 additions & 29 deletions

File tree

documents/vol1-fundamentals/c_tutorials/07B-pointers-arrays-const.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,13 @@ const int* linear_search(const int* data, size_t count, int target) {
304304
}
305305
```
306306

307-
终端应当输出
307+
运行后你会看到类似这样的输出(地址那串数字每次运行都不一样,这是系统的地址随机化 ASLR 在起作用,属正常现象)
308308

309309
```text
310-
找到了目标 43,位于地址 00000000005ffe5c,是数组的第 3 个元素。
310+
找到了目标 43,位于地址 0x7ffd02b50bac,是数组的第 3 个元素。
311311
```
312312

313-
如果出现乱码,可能是字符编码的问题
313+
地址本身不用记,关键是后面那个「第 3 个元素」——`result - arr` 算出来就是 `3`,这个结论是稳的。顺带一提:要是终端里中文显示成乱码,那是终端编码不是 UTF-8 的问题,跟代码无关,换 WSL2 或现代终端就好。
314314

315315
### 练习 2:指针版数组反转
316316

@@ -326,6 +326,8 @@ void reverse_array(int* data, size_t count);
326326
### 练习 2 参考答案
327327
328328
```c
329+
#include <stdio.h>
330+
329331
void reverse_array(int* data, size_t count);
330332
331333
int main(void) {
@@ -422,13 +424,15 @@ const int* p1 = &value;
422424
int* const p2 = &value;
423425
const int* const p3 = &value;
424426

425-
*p1 = other; //不合法,因为p1是指向 const int 类型的指针,无法修改其值
426-
*p2 = other; //合法,因为p2是指向 int 类型的 const 指针,不能指向其它地址,但可以修改其值
427-
*p3 = other; //不合法,因为p3既是指向 const int 类型的指针,也是 const 指针,所以既不能指向其它地址,也不能修改其值
428-
429-
p1 = &other; //合法,因为p1不是 const 类型的指针,所以可以指向其它地址
430-
p2 = &other; //不合法,因为p2是指向 int 类型的 const 指针,不能指向其它地址
431-
p3 = &other; //不合法,因为p3既是指向 const int 类型的指针,也是 const 指针,所以既不能指向其它地址,也不能修改其值
427+
// 下面六行用来判断合法性,逐行对照上面的声明看(都写成注释,
428+
// 因为不合法的那几行真写出来会编译报错):
429+
// *p1 = other; // 不合法:p1 指向 const int,无法通过它改值
430+
// *p2 = other; // 合法:p2 是 const 指针(指向固定),但它指向的是普通 int,值能改
431+
// *p3 = other; // 不合法:p3 既指向 const int,自身也是 const,改值改向都不行
432+
//
433+
// p1 = &other; // 合法:p1 本身不是 const 指针,可以改指向
434+
// p2 = &other; // 不合法:p2 是 const 指针,指向不能改
435+
// p3 = &other; // 不合法:p3 同样是 const 指针,指向不能改
432436

433437
```
434438

documents/vol1-fundamentals/c_tutorials/08A-multi-level-pointers.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ int** allocate_matrix(int rows, int cols) {
314314
return NULL;
315315
}
316316
317-
int** matrix= malloc(sizeof(int*) * rows);
317+
int** matrix = malloc(sizeof(int*) * rows);
318318
if (matrix == NULL) {
319319
return NULL; //防止内存分配失败,但这不太可能吧...以防万一,还是写上吧,如果程序在1978年的硬件上跑呢(笑)
320320
}
@@ -358,24 +358,25 @@ void fill_matrix(int** matrix, int rows, int cols, int value) {
358358
```
359359

360360
```text
361-
兴致大发,来写一个图示awa
362-
[M](这个是matrix)(int** 类型)
363-
[M_rows_i](这个是matrix[rows])(int* 类型)
364-
[Value](这个是matrix的值)(int类型)
365-
366-
[M]--->[M_rows_0][M_rows_1][M_rows_2]...[M_rows_n]
367-
| | | | |
368-
↓ |—————————|—————————|—————————————|————>第2次解引用
369-
第1次解引用 ↓ ↓ ↓ ↓
370-
[Value] [Value] [Value] [Value]
371-
[Value] [Value] [Value] [Value]
372-
[Value] [Value] [Value] [Value]
373-
[Value] [Value] [Value] [Value]
374-
[Value] [Value] [Value] [Value]
375-
......
376-
[Value] [Value] [Value] [Value]
377-
378-
应该写的很清楚吧OwO?
361+
兴致大发,来画个图示 awa
362+
363+
matrix 的类型是 int**,要解两次引用才摸到具体的 int:
364+
365+
matrix (int**)
366+
367+
│ matrix[0] matrix[1] matrix[2] ... matrix[n] ← 第 1 次解引用,每个是 int*
368+
│ │ │ │ │
369+
│ ▼ ▼ ▼ ▼
370+
└─▶ ┌─┬─┬─┬─┐ ┌─┬─┬─┬─┐ ┌─┬─┬─┬─┐ ┌─┬─┐
371+
│ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ... │ │ │ ← 第 2 次解引用,每个是 int
372+
└─┴─┴─┴─┘ └─┴─┴─┴─┘ └─┴─┴─┴─┘ └─┴─┘
373+
374+
matrix[0][1] 就是这一格(第 0 行、第 1 列的那个 int)
375+
376+
- matrix[i] :int*,指向第 i 行的开头(那块 cols 个 int)
377+
- matrix[i][j] :int,第 i 行第 j 列的具体值
378+
379+
这下应该清楚了吧 OwO?
379380
```
380381

381382
提示:分配时先分配一个指针数组(`int**` 指向的那一维),然后对每一行分别 `malloc`。释放时顺序反过来——先释放每一行,再释放指针数组本身。

0 commit comments

Comments
 (0)