2024-07-31 01:20:24 +00:00
|
|
|
|
import{_ as s,c as a,o as i,a6 as n}from"./chunks/framework.CZKtKhAm.js";const t="/c/assets/1.L8V3GBrc.png",l="/c/assets/2.CdvhiwcU.png",e="/c/assets/3.D74t3-Xt.png",d="/c/assets/4.DCQsqYYC.gif",p="/c/assets/5.CUjMoXNF.gif",r="/c/assets/6.BWbajkuB.gif",c="/c/assets/7.C1JnBDr7.png",o="/c/assets/8.DwN2p3bo.png",h="/c/assets/9.DdSfz-Pf.gif",k="/c/assets/10.BMocjVt7.gif",B=JSON.parse('{"title":"第一章:颇具争议的指针","description":"","frontmatter":{},"headers":[],"relativePath":"notes/01_c-basic/06_xdx/index.md","filePath":"notes/01_c-basic/06_xdx/index.md","lastUpdated":1722388783000}'),u={name:"notes/01_c-basic/06_xdx/index.md"},g=n('<blockquote><ul><li><code>指针</code>是 C 语言中<code>最重要</code>的概念之一,也是<code>最难以理解</code>的概念之一。</li><li><code>指针</code>是 C 语言的<code>精髓</code>,要想掌握 C 语言就需要深入地了解指针。</li></ul></blockquote><h1 id="第一章-颇具争议的指针" tabindex="-1">第一章:颇具争议的指针 <a class="header-anchor" href="#第一章-颇具争议的指针" aria-label="Permalink to "第一章:颇具争议的指针""></a></h1><h2 id="_1-1-概述" tabindex="-1">1.1 概述 <a class="header-anchor" href="#_1-1-概述" aria-label="Permalink to "1.1 概述""></a></h2><ul><li>目前而言,操作系统几乎都是通过 C 语言来编写和维护的;而 C 语言提供了指针的用法,其能直接操作内存地址,是个非常<code>强大</code>和<code>灵活</code>的工具;但是,需要开发者<code>小心谨慎的使用</code>,以确保程序的稳定性和安全性。</li></ul><div class="note custom-block github-alert"><p class="custom-block-title">NOTE</p><p></p><p>之所以指针在 C 语言中颇具争议,是因为一方面其功能强大,直接操作内存地址;另一方面,又很危险,不正确的使用指针的方式,非常容易导致程序崩溃。</p></div><ul><li><p>如果没有能很好的使用指针,就会带来一系列的问题,如:</p><ul><li>① <strong>空指针引用(Null Pointer Dereference)</strong>:当一个指针没有正确初始化或者被赋予了空(NULL)值时,如果程序尝试访问该指针所指向的内存,会导致运行时错误,甚至导致程序崩溃。</li><li>② <strong>野指针(Dangling Pointers)</strong>:指针指向的内存地址曾经分配给某个变量或对象,但后来该变量或对象被释放或者移动,导致指针仍指向已经无效的内存位置。对野指针进行操作可能会导致未定义的行为或程序崩溃。</li><li>③ <strong>指针算术错误</strong>:在进行指针运算时,如果没有正确管理指针的偏移量或者超出了数组的边界,可能会导致指针指向错误的内存位置,从而影响程序的正确性和安全性。</li><li>④ <strong>内存泄漏</strong>:如果动态分配的内存通过指针分配,但在不再需要时没有正确释放,会导致内存泄漏,长时间运行的程序可能会耗尽系统资源。</li></ul></li><li><p>为了减少指针带来的风险,开发人员可以采取以下的措施:</p><ul><li>① <strong>良好的编程实践</strong>:确保指针的初始化和使用是安全的,避免空指针引用和野指针问题。</li><li>② <strong>边界检查</strong>:在进行指针运算时,始终确保不会超出数组或内存分配的边界。</li><li>③ <strong>使用指针和引用的适当性</strong>:在可能的情况下,可以考虑使用更安全的语言特性,如:引用(在 C++ 等编程语言中)或者更高级别的数据结构来代替裸指针,从而减少指针使用时的潜在风险。</li></ul></li></ul><div class="important custom-block github-alert"><p class="custom-block-title">IMPORTANT</p><p></p><ul><li>① 既然指针很危险,那么通过一系列的手段将指针包装或屏蔽,以达到程序安全的目的(这是现代化的高级编程语言解决的思路,如:Java、Go、Rust 等)。</li><li><EFBFBD><EFBFBD>
|
2024-07-30 05:13:16 +00:00
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 项目名称和版本号</span></span>
|
|
|
|
|
<span class="line"><span>project(c-study VERSION 1.0 LANGUAGES C)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 设置 C 标准</span></span>
|
|
|
|
|
<span class="line"><span>set(CMAKE_C_STANDARD 23)</span></span>
|
|
|
|
|
<span class="line"><span>set(CMAKE_C_STANDARD_REQUIRED True)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 辅助函数,用于递归查找所有源文件</span></span>
|
|
|
|
|
<span class="line"><span>function(collect_sources result dir)</span></span>
|
|
|
|
|
<span class="line"><span> file(GLOB_RECURSE new_sources "\${dir}/*.c")</span></span>
|
|
|
|
|
<span class="line"><span> set(\${result} \${\${result}} \${new_sources} PARENT_SCOPE)</span></span>
|
|
|
|
|
<span class="line"><span>endfunction()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 查找顶层 include 目录(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span>if (EXISTS "\${CMAKE_SOURCE_DIR}/include")</span></span>
|
|
|
|
|
<span class="line"><span> include_directories(\${CMAKE_SOURCE_DIR}/include)</span></span>
|
|
|
|
|
<span class="line"><span>endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 查找所有源文件</span></span>
|
|
|
|
|
<span class="line"><span>set(SOURCES)</span></span>
|
|
|
|
|
<span class="line"><span>collect_sources(SOURCES \${CMAKE_SOURCE_DIR})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 用于存储已经处理过的可执行文件名,防止重复</span></span>
|
|
|
|
|
<span class="line"><span>set(EXECUTABLE_NAMES)</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span># 创建可执行文件</span></span>
|
|
|
|
|
<span class="line"><span>foreach (SOURCE \${SOURCES})</span></span>
|
|
|
|
|
<span class="line"><span> # 获取文件的相对路径</span></span>
|
|
|
|
|
<span class="line"><span> file(RELATIVE_PATH REL_PATH \${CMAKE_SOURCE_DIR} \${SOURCE})</span></span>
|
|
|
|
|
<span class="line"><span> # 将路径中的斜杠替换为下划线,生成唯一的可执行文件名</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "/" "_" EXECUTABLE_NAME \${REL_PATH})</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "\\\\" "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span> string(REPLACE "." "_" EXECUTABLE_NAME \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 处理与 CMakeLists.txt 文件同名的问题</span></span>
|
|
|
|
|
<span class="line"><span> if (\${EXECUTABLE_NAME} STREQUAL "CMakeLists_txt")</span></span>
|
|
|
|
|
<span class="line"><span> set(EXECUTABLE_NAME "\${EXECUTABLE_NAME}_exec")</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查是否已经创建过同名的可执行文件</span></span>
|
|
|
|
|
<span class="line"><span> if (NOT EXECUTABLE_NAME IN_LIST EXECUTABLE_NAMES)</span></span>
|
|
|
|
|
<span class="line"><span> list(APPEND EXECUTABLE_NAMES \${EXECUTABLE_NAME})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 创建可执行文件</span></span>
|
|
|
|
|
<span class="line"><span> add_executable(\${EXECUTABLE_NAME} \${SOURCE})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 查找源文件所在的目录,并添加为包含目录(头文件可能在同一目录下)</span></span>
|
|
|
|
|
<span class="line"><span> get_filename_component(DIR \${SOURCE} DIRECTORY)</span></span>
|
|
|
|
|
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR})</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查并添加子目录中的 include 目录(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span> if (EXISTS "\${DIR}/include")</span></span>
|
|
|
|
|
<span class="line"><span> target_include_directories(\${EXECUTABLE_NAME} PRIVATE \${DIR}/include)</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span></span></span>
|
|
|
|
|
<span class="line"><span> # 检查并添加 module 目录中的所有 C 文件(如果存在)</span></span>
|
|
|
|
|
<span class="line"><span> if (EXISTS "\${DIR}/module")</span></span>
|
|
|
|
|
<span class="line"><span> file(GLOB_RECURSE MODULE_SOURCES "\${DIR}/module/*.c")</span></span>
|
|
|
|
|
<span class="line"><span> target_sources(\${EXECUTABLE_NAME} PRIVATE \${MODULE_SOURCES})</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
|
|
|
|
<span class="line"><span> endif ()</span></span>
|
2024-07-31 01:20:24 +00:00
|
|
|
|
<span class="line"><span>endforeach ()</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br></div></div><ul><li>在 CLion 中正常运行代码:</li></ul><p><img src="`+h+'" alt=""></p><ul><li>在 CLion 中通过 valgrind 运行代码:</li></ul><p><img src="'+k+`" alt=""></p><h1 id="第四章-指针的理解和定义-⭐" tabindex="-1">第四章:指针的理解和定义(⭐) <a class="header-anchor" href="#第四章-指针的理解和定义-⭐" aria-label="Permalink to "第四章:指针的理解和定义(⭐)""></a></h1><h2 id="_4-1-概述" tabindex="-1">4.1 概述 <a class="header-anchor" href="#_4-1-概述" aria-label="Permalink to "4.1 概述""></a></h2><h1 id="第五章-指针的运算-⭐" tabindex="-1">第五章:指针的运算(⭐) <a class="header-anchor" href="#第五章-指针的运算-⭐" aria-label="Permalink to "第五章:指针的运算(⭐)""></a></h1><h2 id="_5-1-概述" tabindex="-1">5.1 概述 <a class="header-anchor" href="#_5-1-概述" aria-label="Permalink to "5.1 概述""></a></h2><h2 id="_5-2-总结" tabindex="-1">5.2 总结 <a class="header-anchor" href="#_5-2-总结" aria-label="Permalink to "5.2 总结""></a></h2><ul><li><p>在 C 语言中,<code>普通变量</code>是直接存储<code>数据</code>的<code>变量</code>。对于普通变量,支持的操作包括:</p><ul><li>① <strong>赋值操作</strong>:给变量赋值,如:<code>int a = 5</code>。</li><li>② <strong>算术运算</strong>:可以对数值类型的普通
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ptr </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> arr;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 这句话是合法的,ptr现在指向arr[0]</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%p\\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, arr);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 打印数组名,会打印数组首地址</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%p\\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">arr</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 打印第一个元素的地址</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li><li><p><strong>数组名是常量指针</strong>: 数组名是一个常量指针,不能改变它指向的位置,而指针变量可以改变它指向的位置。</p><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> arr</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">];</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ptr </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> arr;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 合法,ptr指向arr[0]</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ptr</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">++</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 合法,ptr现在指向arr[1]</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">// arr++; // 非法,编译错误,因为数组名是常量,不能改变</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li><li><p><strong>sizeof运算符的结果不同</strong>: 使用<code>sizeof</code>运算符对数组名和指针变量会得到不同的结果,数组名会返回整个数组的大小,而指针变量会返回指针本身的大小。</p><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> arr</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">];</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ptr </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> arr;</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"sizeof(arr) = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%lu\\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">sizeof</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(arr));</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 返回数组的大小,5 * sizeof(int)</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"sizeof(ptr) = </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%lu\\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">sizeof</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(ptr));</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 返回指针的大小,通常是4或8字节</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li><li><p><strong>地址运算符的结果不同</strong>: 使用地址运算符<code>&</code>对数组名和指针变量会得到不同的结果,对数组名使用<code>&</code>会返回数组的地址,而对指针变量使用<code>&</code>会返回指针变量本身的地址。</p><div class="language-c vp-adaptive-theme line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">c</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;"> arr</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">];</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">int</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> *</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">ptr </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> arr;</span></span>
|
|
|
|
|
<span class="line"></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Address of array: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%p\\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">arr</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 返回整个数组的地址</span></span>
|
|
|
|
|
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">printf</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Address of pointer: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">%p\\n</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">&</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">ptr</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // 返回指针变量ptr的地址</span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li></ol><p>综上所述,通过这些示例和解释,可以看出数组名虽然在某些场合下可以像指针一样使用,但它并不是一个真正的指针变量,而是一个常量,表示数组的首地址。</p>`,72),E=[g];function b(m,C,y,F,_,A){return i(),a("div",null,E)}const q=s(u,[["render",b]]);export{B as __pageData,q as default};
|