class="tags" href="/C.html" title=
c >
c ontent_views"
c lass="tags" href="/C.html" title=
c >
c lass="markdown_views prism-tomorrow-night">
class="tags" href="/C.html" title=c >c ap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-bloc lass="tags" href="/C.html" title=c >c k" style="-webkit-tap-highlight-c lass="tags" href="/C.html" title=c >c olor: rgba(0, 0, 0, 0);">
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/c lass="tags" href="/C.html" title=c >c b7f73c lass="tags" href="/C.html" title=c >c da1a14130840e356c lass="tags" href="/C.html" title=c >c b0b6f9e4.png#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" width="100" />
<
c lass="tags" href="/C.html" title=
c >
c enter>
博客主页: [小ᶻ☡꙳ᵃⁱᵍᶜ꙳]
c lass="tags" href="/C.html" title=
c >
c enter>
<
c lass="tags" href="/C.html" title=
c >
c enter>
本文专栏: C语言
c lass="tags" href="/C.html" title=
c >
c enter>
class="tags" href="/C.html" title=c >c ="https://img-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" width="1000" height="100" />
💯前言
C语言 作为一门经典的编程语言 c lass="tags" href="/C.html" title=c >c ;以其高效 、<c lass="tags" href="/C.html" title=c >c ode>灵活c lass="tags" href="/C.html" title=c >c ode>和接近底层硬件 的特点c lass="tags" href="/C.html" title=c >c ;广泛应用于嵌入式开发 、操作系统内核 以及高性能计算 等领域。然而c lass="tags" href="/C.html" title=c >c ;C语言的灵活性 也带来了学习过程中的诸多挑战 c lass="tags" href="/C.html" title=c >c ;特别是在指针与数组 、<c lass="tags" href="/C.html" title=c >c ode>内存管理c lass="tags" href="/C.html" title=c >c ode>以及字符串处理 等核心概念的掌握上。 本文通过对经典题目 的逐步解析c lass="tags" href="/C.html" title=c >c ;深入探讨 C语言 的关键特性 c lass="tags" href="/C.html" title=c >c ;并结合<c lass="tags" href="/C.html" title=c >c ode>代码实例c lass="tags" href="/C.html" title=c >c ode>详细讲解其底层原理 与实际应用 。希望通过这样的方式c lass="tags" href="/C.html" title=c >c ;帮助读者更全面地理解 C语言的强大之处 c lass="tags" href="/C.html" title=c >c ;从而在编程中更加得心应手 。 C语言 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/2562ddc lass="tags" href="/C.html" title=c >c d78164994b20b50c lass="tags" href="/C.html" title=c >c 5c lass="tags" href="/C.html" title=c >c d0130e3.png#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" />
💯题目一
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/e40af9b5e2534029a8058301163ec lass="tags" href="/C.html" title=c >c aaa.png" alt="在这里插入图片描述" /> 题目描述:
如下程序段的输出结果为:
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">class="tags" href="/C.html" title=c >c lass="token keyword">unsigned class="tags" href="/C.html" title=c >c lass="token keyword">long pulArrayclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">[ class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">] class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ class="tags" href="/C.html" title=c >c lass="token number">6 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token number">7 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token number">8 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token number">9 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token number">10 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">} class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token keyword">unsigned class="tags" href="/C.html" title=c >c lass="token keyword">long class="tags" href="/C.html" title=c >c lass="token operator">* pulPtrclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
pulPtr class="tags" href="/C.html" title=c >c lass="token operator">= pulArrayclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token operator">* class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( pulPtr class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token number">3 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token operator">+= class="tags" href="/C.html" title=c >c lass="token number">3 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">printf class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token string">"%d, %d\n" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token operator">* pulPtrclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token operator">* class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( pulPtr class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token number">3 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
详细分析与解答
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 这道题考察的是指针和数组的操作c lass="tags" href="/C.html" title=c >c ;特别是指针的算术运算以及对数组元素的修改。接下来c lass="tags" href="/C.html" title=c >c ;我们逐步分析程序的执行过程c lass="tags" href="/C.html" title=c >c ;以得出正确的答案。
代码逐步解析
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 首先c lass="tags" href="/C.html" title=c >c ;我们逐行解释代码c lass="tags" href="/C.html" title=c >c ;并深入理解每一步的执行过程。
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">class="tags" href="/C.html" title=c >c lass="token keyword">unsigned class="tags" href="/C.html" title=c >c lass="token keyword">long pulArrayclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">[ class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">] class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ class="tags" href="/C.html" title=c >c lass="token number">6 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token number">7 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token number">8 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token number">9 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token number">10 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">} class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
这一行定义了一个 <c lass="tags" href="/C.html" title=c >c ode>unsigned longc lass="tags" href="/C.html" title=c >c ode> 类型的数组 <c lass="tags" href="/C.html" title=c >c ode>pulArrayc lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;它包含 5 个元素c lass="tags" href="/C.html" title=c >c ;分别是 <c lass="tags" href="/C.html" title=c >c ode>{6, 7, 8, 9, 10}c lass="tags" href="/C.html" title=c >c ode>。数组在内存中会被分配一块连续的存储空间c lass="tags" href="/C.html" title=c >c ;每个元素占据一个存储单元c lass="tags" href="/C.html" title=c >c ;内存地址按元素依次递增。
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">class="tags" href="/C.html" title=c >c lass="token keyword">unsigned class="tags" href="/C.html" title=c >c lass="token keyword">long class="tags" href="/C.html" title=c >c lass="token operator">* pulPtrclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
这里定义了一个 <c lass="tags" href="/C.html" title=c >c ode>unsigned longc lass="tags" href="/C.html" title=c >c ode> 类型的指针 <c lass="tags" href="/C.html" title=c >c ode>pulPtrc lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;它将用于指向数组中的元素。
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">pulPtr class="tags" href="/C.html" title=c >c lass="token operator">= pulArrayclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
这行代码将 <c lass="tags" href="/C.html" title=c >c ode>pulPtrc lass="tags" href="/C.html" title=c >c ode> 指向数组 <c lass="tags" href="/C.html" title=c >c ode>pulArrayc lass="tags" href="/C.html" title=c >c ode> 的首地址。也就是说c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>pulPtrc lass="tags" href="/C.html" title=c >c ode> 现在指向 <c lass="tags" href="/C.html" title=c >c ode>pulArray[0]c lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;即值为 <c lass="tags" href="/C.html" title=c >c ode>6c lass="tags" href="/C.html" title=c >c ode> 的位置。
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">class="tags" href="/C.html" title=c >c lass="token operator">* class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( pulPtr class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token number">3 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token operator">+= class="tags" href="/C.html" title=c >c lass="token number">3 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
这一行代码对数组中的一个元素进行了修改。首先c lass="tags" href="/C.html" title=c >c ;我们来看 <c lass="tags" href="/C.html" title=c >c ode>pulPtr + 3c lass="tags" href="/C.html" title=c >c ode>:
<c lass="tags" href="/C.html" title=c >c ode>pulPtrc lass="tags" href="/C.html" title=c >c ode> 是指向数组第一个元素的指针。 <c lass="tags" href="/C.html" title=c >c ode>pulPtr + 3c lass="tags" href="/C.html" title=c >c ode> 表示将指针偏移到数组中第 4 个元素的地址(注意数组的索引是从 0 开始的)。
通过 <c lass="tags" href="/C.html" title=c >c ode>*(pulPtr + 3)c lass="tags" href="/C.html" title=c >c ode> 取出该地址所指向的值c lass="tags" href="/C.html" title=c >c ;也就是数组的第 4 个元素 <c lass="tags" href="/C.html" title=c >c ode>9c lass="tags" href="/C.html" title=c >c ode>。然后 <c lass="tags" href="/C.html" title=c >c ode>+= 3c lass="tags" href="/C.html" title=c >c ode> 操作将该值增加 3c lass="tags" href="/C.html" title=c >c ;变为 <c lass="tags" href="/C.html" title=c >c ode>12c lass="tags" href="/C.html" title=c >c ode>。因此c lass="tags" href="/C.html" title=c >c ;数组 <c lass="tags" href="/C.html" title=c >c ode>pulArrayc lass="tags" href="/C.html" title=c >c ode> 现在的值变成了 <c lass="tags" href="/C.html" title=c >c ode>{6, 7, 8, 12, 10}c lass="tags" href="/C.html" title=c >c ode>。
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">printf class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token string">"%d, %d\n" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token operator">* pulPtrclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token operator">* class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( pulPtr class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token number">3 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
<c lass="tags" href="/C.html" title=c >c ode>*pulPtrc lass="tags" href="/C.html" title=c >c ode> 取出指针 <c lass="tags" href="/C.html" title=c >c ode>pulPtrc lass="tags" href="/C.html" title=c >c ode> 所指向的值c lass="tags" href="/C.html" title=c >c ;即数组的第一个元素 <c lass="tags" href="/C.html" title=c >c ode>6c lass="tags" href="/C.html" title=c >c ode>。 <c lass="tags" href="/C.html" title=c >c ode>*(pulPtr + 3)c lass="tags" href="/C.html" title=c >c ode> 取出指向第 4 个元素的值c lass="tags" href="/C.html" title=c >c ;现在这个值已经被修改为 <c lass="tags" href="/C.html" title=c >c ode>12c lass="tags" href="/C.html" title=c >c ode>。
因此c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>printfc lass="tags" href="/C.html" title=c >c ode> 最终输出的结果是:
<c lass="tags" href="/C.html" title=c >c ode>6, 12
c lass="tags" href="/C.html" title=c >c ode>
💯进一步优化和拓展
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 为了更好地理解这个题目c lass="tags" href="/C.html" title=c >c ;我们来探讨一些与此相关的重要概念c lass="tags" href="/C.html" title=c >c ;包括指针运算、数组与指针的关系c lass="tags" href="/C.html" title=c >c ;以及 C 语言中的一些常见错误和陷阱。
1. 指针与数组的关系
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" /> 在 C 语言中c lass="tags" href="/C.html" title=c >c ;数组名在大多数情况下会退化为指向其首元素的指针。这意味着 <c lass="tags" href="/C.html" title=c >c ode>pulArrayc lass="tags" href="/C.html" title=c >c ode> 可以被看作是指向 <c lass="tags" href="/C.html" title=c >c ode>pulArray[0]c lass="tags" href="/C.html" title=c >c ode> 的地址。因此c lass="tags" href="/C.html" title=c >c ;以下代码:
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">pulPtr class="tags" href="/C.html" title=c >c lass="token operator">= pulArrayclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
c lass="tags" href="/C.html" title=c >c ode>
可以理解为让指针 <c lass="tags" href="/C.html" title=c >c ode>pulPtrc lass="tags" href="/C.html" title=c >c ode> 指向数组的起始地址。此后c lass="tags" href="/C.html" title=c >c ;通过指针的偏移量我们可以访问数组中的其他元素c lass="tags" href="/C.html" title=c >c ;例如:
<c lass="tags" href="/C.html" title=c >c ode>pulPtr + 1c lass="tags" href="/C.html" title=c >c ode> 指向 <c lass="tags" href="/C.html" title=c >c ode>pulArray[1]c lass="tags" href="/C.html" title=c >c ode> <c lass="tags" href="/C.html" title=c >c ode>pulPtr + 3c lass="tags" href="/C.html" title=c >c ode> 指向 <c lass="tags" href="/C.html" title=c >c ode>pulArray[3]c lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;即值为 <c lass="tags" href="/C.html" title=c >c ode>9c lass="tags" href="/C.html" title=c >c ode> 的位置
2. 指针运算的注意事项
指针的运算是基于指针所指向的数据类型大小的。例如c lass="tags" href="/C.html" title=c >c ;对于一个 <c lass="tags" href="/C.html" title=c >c ode>unsigned longc lass="tags" href="/C.html" title=c >c ode> 类型的指针c lass="tags" href="/C.html" title=c >c ;每次加 <c lass="tags" href="/C.html" title=c >c ode>1c lass="tags" href="/C.html" title=c >c ode> 实际上会增加 <c lass="tags" href="/C.html" title=c >c ode>sizeof(unsigned long)c lass="tags" href="/C.html" title=c >c ode> 个字节c lass="tags" href="/C.html" title=c >c ;而不是简单地增加 1 个字节。这种特性在访问数组元素时尤为重要c lass="tags" href="/C.html" title=c >c ;因为它能保证指针准确地指向下一个元素c lass="tags" href="/C.html" title=c >c ;而不是指向任意的中间位置。
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
3. 常见的错误和陷阱
数组越界访问 :在使用指针操作数组时c lass="tags" href="/C.html" title=c >c ;如果偏移量超出了数组的边界c lass="tags" href="/C.html" title=c >c ;就会发生越界访问c lass="tags" href="/C.html" title=c >c ;导致未定义行为。这种错误在 C 语言中非常常见且很难调试。未初始化指针 :如果指针没有正确初始化(例如没有指向有效的内存地址)c lass="tags" href="/C.html" title=c >c ;对其解引用会导致严重的错误c lass="tags" href="/C.html" title=c >c ;例如段错误(<c lass="tags" href="/C.html" title=c >c ode>Segmentation Faultc lass="tags" href="/C.html" title=c >c ode>)。在这道题中c lass="tags" href="/C.html" title=c >c ;指针 <c lass="tags" href="/C.html" title=c >c ode>pulPtrc lass="tags" href="/C.html" title=c >c ode> 在赋值之前指向了有效的数组c lass="tags" href="/C.html" title=c >c ;因此没有这个问题。
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
4. 拓展:指针操作的应用场景
指针的运算和数组的结合在许多场景中都非常有用c lass="tags" href="/C.html" title=c >c ;例如:
遍历数组 :通过指针偏移量来遍历数组中的元素c lass="tags" href="/C.html" title=c >c ;通常比使用数组索引更加高效c lass="tags" href="/C.html" title=c >c ;尤其是在嵌入式系统中c lass="tags" href="/C.html" title=c >c ;由于资源有限c lass="tags" href="/C.html" title=c >c ;高效地访问数据显得尤为重要。动态内存管理 :在堆上分配内存时(例如使用 <c lass="tags" href="/C.html" title=c >c ode>malloc lass="tags" href="/C.html" title=c >c c lass="tags" href="/C.html" title=c >c ode>)c lass="tags" href="/C.html" title=c >c ;返回的是指向已分配内存的指针c lass="tags" href="/C.html" title=c >c ;之后对这块内存的操作大多需要通过指针进行。字符处理与字符串操作 :指针在字符串处理方面非常高效c lass="tags" href="/C.html" title=c >c ;通过指针可以直接访问字符数组中的每一个字符c lass="tags" href="/C.html" title=c >c ;进行遍历、修改等操作。 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
5. 指针与内存地址的深入理解
指针不仅仅是一个变量c lass="tags" href="/C.html" title=c >c ;它实际上是一个存储内存地址的变量。理解指针与内存地址之间的关系c lass="tags" href="/C.html" title=c >c ;对于掌握指针的高级用法至关重要。例如c lass="tags" href="/C.html" title=c >c ;指针的偏移量取决于它所指向的数据类型大小c lass="tags" href="/C.html" title=c >c ;这使得 C 语言中的指针非常灵活且强大。在涉及复杂数据结构(如链表、树结构)时c lass="tags" href="/C.html" title=c >c ;指针的使用必不可少c lass="tags" href="/C.html" title=c >c ;可以有效地管理和操纵动态数据。 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
💯题目二
在另一段代码中c lass="tags" href="/C.html" title=c >c ;定义了一个自定义的字符串长度函数 <c lass="tags" href="/C.html" title=c >c ode>my_strlenc lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;其目的是计算一个字符串的长度。
代码如下:
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">class="tags" href="/C.html" title=c >c lass="token keyword">int class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">my_strlen class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c har class="tags" href="/C.html" title=c >c lass="token operator">* arrclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ class="tags" href="/C.html" title=c >c lass="token keyword">int c lass="tags" href="/C.html" title=c >c ount class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">0 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token keyword">while class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token operator">* arr class="tags" href="/C.html" title=c >c lass="token operator">!= class="tags" href="/C.html" title=c >c lass="token string">'\0' class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ c lass="tags" href="/C.html" title=c >c ountclass="tags" href="/C.html" title=c >c lass="token operator">++ class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; arrclass="tags" href="/C.html" title=c >c lass="token operator">++ class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">} class="tags" href="/C.html" title=c >c lass="token keyword">return c lass="tags" href="/C.html" title=c >c ountclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">} class="tags" href="/C.html" title=c >c lass="token keyword">int class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">main3 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c har arrclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">[ class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">] class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token string">"今天是2024年11月26日" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">printf class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token string">"%d" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">my_strlen class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( arrclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; class="tags" href="/C.html" title=c >c lass="token keyword">return class="tags" href="/C.html" title=c >c lass="token number">0 class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">}
c lass="tags" href="/C.html" title=c >c ode>
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
数组传递给函数时的特性
在 <c lass="tags" href="/C.html" title=c >c ode>main3()c lass="tags" href="/C.html" title=c >c ode> 函数中c lass="tags" href="/C.html" title=c >c ;定义了一个字符数组 <c lass="tags" href="/C.html" title=c >c ode>arrc lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;并将其传递给了 <c lass="tags" href="/C.html" title=c >c ode>my_strlenc lass="tags" href="/C.html" title=c >c ode> 函数。这个过程中c lass="tags" href="/C.html" title=c >c ;我们并不需要显式定义一个指针c lass="tags" href="/C.html" title=c >c ;原因在于 C 语言中的数组退化特性。 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
数组退化为指针
在调用 <c lass="tags" href="/C.html" title=c >c ode>my_strlen(arr)c lass="tags" href="/C.html" title=c >c ode> 时c lass="tags" href="/C.html" title=c >c ;数组名 <c lass="tags" href="/C.html" title=c >c ode>arrc lass="tags" href="/C.html" title=c >c ode> 会自动退化为一个指向其首元素的指针。也就是说c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>my_strlenc lass="tags" href="/C.html" title=c >c ode> 实际上接收的是 <c lass="tags" href="/C.html" title=c >c ode>arrc lass="tags" href="/C.html" title=c >c ode> 的首地址(一个 <c lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c har*c lass="tags" href="/C.html" title=c >c ode> 类型的指针)c lass="tags" href="/C.html" title=c >c ;这使得 <c lass="tags" href="/C.html" title=c >c ode>my_strlenc lass="tags" href="/C.html" title=c >c ode> 可以遍历整个字符串直到遇到字符串的终止符 <c lass="tags" href="/C.html" title=c >c ode>\0c lass="tags" href="/C.html" title=c >c ode>。 这种退化机制在 C 语言中被广泛使用c lass="tags" href="/C.html" title=c >c ;使得函数可以方便地操作数组数据c lass="tags" href="/C.html" title=c >c ;而不需要将整个数组拷贝传递c lass="tags" href="/C.html" title=c >c ;这样也提高了效率。 此外c lass="tags" href="/C.html" title=c >c ;数组退化为指针的特性在很多场景下可以提高代码的通用性和复用性c lass="tags" href="/C.html" title=c >c ;因为指针可以指向不同的数组c lass="tags" href="/C.html" title=c >c ;从而实现对不同数据的操作。 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
指针遍历字符串
在 <c lass="tags" href="/C.html" title=c >c ode>my_strlenc lass="tags" href="/C.html" title=c >c ode> 函数中c lass="tags" href="/C.html" title=c >c ;使用指针 <c lass="tags" href="/C.html" title=c >c ode>arrc lass="tags" href="/C.html" title=c >c ode> 来遍历整个字符串:
<c lass="tags" href="/C.html" title=c >c ode c lass="tags" href="/C.html" title=c >c lass="prism language-c lass="tags" href="/C.html" title=c >c ">class="tags" href="/C.html" title=c >c lass="token keyword">while class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( class="tags" href="/C.html" title=c >c lass="token operator">* arr class="tags" href="/C.html" title=c >c lass="token operator">!= class="tags" href="/C.html" title=c >c lass="token string">'\0' class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">) class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">{ c lass="tags" href="/C.html" title=c >c ountclass="tags" href="/C.html" title=c >c lass="token operator">++ class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; arrclass="tags" href="/C.html" title=c >c lass="token operator">++ class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">;
class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">}
c lass="tags" href="/C.html" title=c >c ode>
这里c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>*arrc lass="tags" href="/C.html" title=c >c ode> 取出当前字符的值c lass="tags" href="/C.html" title=c >c ;并判断是否为终止符 <c lass="tags" href="/C.html" title=c >c ode>\0c lass="tags" href="/C.html" title=c >c ode>。如果不是c lass="tags" href="/C.html" title=c >c ;就将 <c lass="tags" href="/C.html" title=c >c ode>arrc lass="tags" href="/C.html" title=c >c ode> 向后移动一个字符位置c lass="tags" href="/C.html" title=c >c ;并增加计数器 <c lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ountc lass="tags" href="/C.html" title=c >c ode>。这种方式让函数能够有效地计算出字符串的长度c lass="tags" href="/C.html" title=c >c ;直到遇到字符串的结束符。 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
输出结果
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/9216f3aec lass="tags" href="/C.html" title=c >c e284327adf7b4b3c lass="tags" href="/C.html" title=c >c d8b838c lass="tags" href="/C.html" title=c >c .png" alt="在这里插入图片描述" />
在 <c lass="tags" href="/C.html" title=c >c ode>main3()c lass="tags" href="/C.html" title=c >c ode> 中c lass="tags" href="/C.html" title=c >c ;字符串为 <c lass="tags" href="/C.html" title=c >c ode>"今天是2024年11月26日"c lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>my_strlen(arr)c lass="tags" href="/C.html" title=c >c ode> 返回 <c lass="tags" href="/C.html" title=c >c ode>20c lass="tags" href="/C.html" title=c >c ode>c lass="tags" href="/C.html" title=c >c ;即字符串的长度。 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
数组与指针的区别
数组是连续内存块 :<c lass="tags" href="/C.html" title=c >c ode>arrc lass="tags" href="/C.html" title=c >c ode> 是一块连续的内存空间c lass="tags" href="/C.html" title=c >c ;包含所有字符。指针是地址变量 :在传递给函数时c lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>arrc lass="tags" href="/C.html" title=c >c ode> 自动退化为指针c lass="tags" href="/C.html" title=c >c ;这个指针指向数组的首元素。通过指针操作c lass="tags" href="/C.html" title=c >c ;可以灵活地遍历或操作数组中的每一个元素。指针的灵活性 :指针可以指向任意内存地址(只要是有效的内存)c lass="tags" href="/C.html" title=c >c ;这使得它在处理动态数据结构(如链表、树等)时特别有用。相比之下c lass="tags" href="/C.html" title=c >c ;数组的内存是固定的c lass="tags" href="/C.html" title=c >c ;数组名只能指向这一块特定的内存。 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
指针与函数参数的灵活性
在许多情况下c lass="tags" href="/C.html" title=c >c ;使用指针来传递参数c lass="tags" href="/C.html" title=c >c ;可以更高效地操作数据。特别是在处理大型数据结构时c lass="tags" href="/C.html" title=c >c ;直接传递数组的首地址比拷贝整个数组更节省内存和时间。此外c lass="tags" href="/C.html" title=c >c ;通过指针c lass="tags" href="/C.html" title=c >c ;我们可以修改数组中的内容c lass="tags" href="/C.html" title=c >c ;而不仅仅是读取它们的值c lass="tags" href="/C.html" title=c >c ;这也是指针传递的一个重要优势。 class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/fc lass="tags" href="/C.html" title=c >c 6ac lass="tags" href="/C.html" title=c >c 6d93b4745b591bb8a8e48f86779.png#pic lass="tags" href="/C.html" title=c >c _right" alt="在这里插入图片描述" width="1" />
💯小结
class="tags" href="/C.html" title=c >c ="https://i-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/36faf3455d4b4a0b990743617f24aaed.png" alt="在这里插入图片描述" /> 在第一段代码中c lass="tags" href="/C.html" title=c >c ;定义指针 <c lass="tags" href="/C.html" title=c >c ode>pulPtrc lass="tags" href="/C.html" title=c >c ode> 让我们可以通过指针偏移 来访问和修改数组中的元素。这种操作在 C 语言 中非常灵活和常用 。
在第二段代码中c lass="tags" href="/C.html" title=c >c ;数组 <c lass="tags" href="/C.html" title=c >c ode>arrc lass="tags" href="/C.html" title=c >c ode> 被传递给 <c lass="tags" href="/C.html" title=c >c ode>my_strlenc lass="tags" href="/C.html" title=c >c ode> 函数c lass="tags" href="/C.html" title=c >c ;数组名自动退化为指针 c lass="tags" href="/C.html" title=c >c ;从而允许函数通过指针遍历整个字符串 。这种机制使得字符串处理非常高效 。
指针与数组 在 C 语言 中是非常重要且强大的工具 c lass="tags" href="/C.html" title=c >c ;能够有效地进行内存操作 和<c lass="tags" href="/C.html" title=c >c ode>数据处理c lass="tags" href="/C.html" title=c >c ode>。在使用时需要小心指针的边界和初始化问题 c lass="tags" href="/C.html" title=c >c ;以避免常见的错误 。
指针的灵活性 使得它在许多场景下成为不可替代的工具 c lass="tags" href="/C.html" title=c >c ;尤其是在涉及复杂数据结构 和动态内存管理 时。
class="tags" href="/C.html" title=c >c ="https://img-blog.c lass="tags" href="/C.html" title=c >c sdnimg.c lass="tags" href="/C.html" title=c >c n/direc lass="tags" href="/C.html" title=c >c t/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" width="1000" height="100" />