From af0a306d1c098b3ca1ae27e292effb61b2c532e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=A4=A7=E4=BB=99?= <1900919313@qq.com> Date: Tue, 22 Oct 2024 01:51:38 +0000 Subject: [PATCH] =?UTF-8?q?2024=E5=B9=B410=E6=9C=8822=E6=97=A5=2009:51?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/notes/02_c-leap/05_xdx/index.md | 45 ++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/docs/notes/02_c-leap/05_xdx/index.md b/docs/notes/02_c-leap/05_xdx/index.md index 27d4805..5c446d2 100644 --- a/docs/notes/02_c-leap/05_xdx/index.md +++ b/docs/notes/02_c-leap/05_xdx/index.md @@ -2052,7 +2052,7 @@ int main() { } ``` -#### 3.3.2.3 指针常量 +#### 3.3.2.3 指针常量(Pointer to Constant) * 语法: @@ -2094,7 +2094,7 @@ int main() { } ``` -#### 3.3.2.4 常量指针 +#### 3.3.2.4 常量指针(Constant Pointer) * 语法: @@ -2130,9 +2130,44 @@ int main() { } ``` -3.3.2.5 指向常量的常量 +#### 3.3.2.5 指向常量的常量指针 (Constant Pointer to Constant) -#### 3.3.2.5 指针常量参数 +* 语法: + +```c +const int* const p3; +``` + +> [!NOTE] +> +> * ① 指向常量的常量指针就是指针的指向和指向的数据值都不能修改。 +> * ② 在实际开发中,较少使用!!! + + + +* 示例: + +```c +#include + +int main() { + + // 禁用 stdout 缓冲区 + setbuf(stdout, nullptr); + + int a = 10; + const int *const p = &a; // 必须在定义时初始化 + + // 错误,不能修改 a 的值 + *p = 20; // [!code error] + // 错误,不能改变指针的指向 + p = &a; // [!code error] + + return 0; +} +``` + +#### 3.3.2.6 指针常量参数 * 在C语言中,单独定义 const 变量没有明显的优势,完全可以使用`#define`命令代替。 @@ -2201,7 +2236,7 @@ int main(){ } ``` -#### 3.3.2.6 深入理解 const 关键字 +#### 3.3.2.7 深入理解 const 关键字 * const 关键字修饰的变量,即:const 常量,一旦被定义就必须初始化,后面的任何赋值行为都将发生错误。