【C语言】指针与数组的例题详解:深入分析与高级用法

devtools/2024/11/28 6:49:25/
class="tags" href="/C.html" title=c>cle class="tags" href="/C.html" title=c>class="baidu_pl">
class="tags" href="/C.html" title=c>cle_class="tags" href="/C.html" title=c>content" class="tags" href="/C.html" title=c>class="articlass="tags" href="/C.html" title=c>cle_class="tags" href="/C.html" title=c>content class="tags" href="/C.html" title=c>clearfix">
class="tags" href="/C.html" title=c>content_views" class="tags" href="/C.html" title=c>class="markdown_views prism-tomorrow-night">class="tags" href="/C.html" title=c>cap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-bloclass="tags" href="/C.html" title=c>ck" style="-webkit-tap-highlight-class="tags" href="/C.html" title=c>color: rgba(0, 0, 0, 0);">

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/class="tags" href="/C.html" title=c>cb7f73class="tags" href="/C.html" title=c>cda1a14130840e356class="tags" href="/C.html" title=c>cb0b6f9e4.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="100" />

<class="tags" href="/C.html" title=c>center> 博客主页: [小ᶻ☡꙳ᵃⁱᵍᶜ꙳] class="tags" href="/C.html" title=c>center> <class="tags" href="/C.html" title=c>center> 本文专栏: C语言 class="tags" href="/C.html" title=c>center>

class="tags" href="/C.html" title=c>class="toclass="tags" href="/C.html" title=c>c">

文章目录

  • 💯前言
  • 💯题目一
    • 详细分析与解答
      • 代码逐步解析
  • 💯进一步优化和拓展
    • 1. 指针与数组的关系
    • 2. 指针运算的注意事项
    • 3. 常见的错误和陷阱
    • 4. 拓展:指针操作的应用场景
    • 5. 指针与内存地址的深入理解
  • 💯题目二
    • 数组传递给函数时的特性
    • 数组退化为指针
    • 指针遍历字符串
    • 输出结果
    • 数组与指针的区别
    • 指针与函数参数的灵活性
  • 💯小结


class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />


💯前言

  • C语言作为一门经典的编程语言class="tags" href="/C.html" title=c>c;以其高效、<class="tags" href="/C.html" title=c>code>灵活class="tags" href="/C.html" title=c>code>和接近底层硬件的特点࿰class="tags" href="/C.html" title=c>c;广泛应用于嵌入式开发操作系统内核以及高性能计算等领域。然而࿰class="tags" href="/C.html" title=c>c;C语言的灵活性也带来了学习过程中的诸多挑战class="tags" href="/C.html" title=c>c;特别是在指针与数组、<class="tags" href="/C.html" title=c>code>内存管理class="tags" href="/C.html" title=c>code>以及字符串处理等核心概念的掌握上。
    本文通过对经典题目的逐步解析࿰class="tags" href="/C.html" title=c>c;深入探讨 C语言关键特性class="tags" href="/C.html" title=c>c;并结合<class="tags" href="/C.html" title=c>code>代码实例class="tags" href="/C.html" title=c>code>详细讲解其底层原理实际应用。希望通过这样的方式࿰class="tags" href="/C.html" title=c>c;帮助读者更全面地理解 C语言的强大之处class="tags" href="/C.html" title=c>c;从而在编程中更加得心应手
    C语言
    class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/2562ddclass="tags" href="/C.html" title=c>cd78164994b20b50class="tags" href="/C.html" title=c>c5class="tags" href="/C.html" title=c>cd0130e3.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" />

💯题目一

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/e40af9b5e2534029a8058301163eclass="tags" href="/C.html" title=c>caaa.png" alt="在这里插入图片描述" />
题目描述:

如下程序段的输出结果为:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">class="tags" href="/C.html" title=c>class="token keyword">unsigned class="tags" href="/C.html" title=c>class="token keyword">long pulArrayclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token number">6class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">7class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">8class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">9class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">10class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token keyword">unsigned class="tags" href="/C.html" title=c>class="token keyword">long class="tags" href="/C.html" title=c>class="token operator">*pulPtrclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
pulPtr class="tags" href="/C.html" title=c>class="token operator">= pulArrayclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token operator">*class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(pulPtr class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">3class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator">+= class="tags" href="/C.html" title=c>class="token number">3class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">printfclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token string">"%d, %d\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token operator">*pulPtrclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token operator">*class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(pulPtr class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">3class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>

详细分析与解答

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />
这道题考察的是指针和数组的操作࿰class="tags" href="/C.html" title=c>c;特别是指针的算术运算以及对数组元素的修改。接下来࿰class="tags" href="/C.html" title=c>c;我们逐步分析程序的执行过程࿰class="tags" href="/C.html" title=c>c;以得出正确的答案。


代码逐步解析

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />
首先࿰class="tags" href="/C.html" title=c>c;我们逐行解释代码࿰class="tags" href="/C.html" title=c>c;并深入理解每一步的执行过程。

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">class="tags" href="/C.html" title=c>class="token keyword">unsigned class="tags" href="/C.html" title=c>class="token keyword">long pulArrayclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token number">6class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">7class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">8class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">9class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token number">10class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>

这一行定义了一个 <class="tags" href="/C.html" title=c>code>unsigned longclass="tags" href="/C.html" title=c>code> 类型的数组 <class="tags" href="/C.html" title=c>code>pulArrayclass="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;它包含 5 个元素࿰class="tags" href="/C.html" title=c>c;分别是 <class="tags" href="/C.html" title=c>code>{6, 7, 8, 9, 10}class="tags" href="/C.html" title=c>code>。数组在内存中会被分配一块连续的存储空间࿰class="tags" href="/C.html" title=c>c;每个元素占据一个存储单元࿰class="tags" href="/C.html" title=c>c;内存地址按元素依次递增。

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">class="tags" href="/C.html" title=c>class="token keyword">unsigned class="tags" href="/C.html" title=c>class="token keyword">long class="tags" href="/C.html" title=c>class="token operator">*pulPtrclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>

这里定义了一个 <class="tags" href="/C.html" title=c>code>unsigned longclass="tags" href="/C.html" title=c>code> 类型的指针 <class="tags" href="/C.html" title=c>code>pulPtrclass="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;它将用于指向数组中的元素。

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">pulPtr class="tags" href="/C.html" title=c>class="token operator">= pulArrayclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>

这行代码将 <class="tags" href="/C.html" title=c>code>pulPtrclass="tags" href="/C.html" title=c>code> 指向数组 <class="tags" href="/C.html" title=c>code>pulArrayclass="tags" href="/C.html" title=c>code> 的首地址。也就是说࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>pulPtrclass="tags" href="/C.html" title=c>code> 现在指向 <class="tags" href="/C.html" title=c>code>pulArray[0]class="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;即值为 <class="tags" href="/C.html" title=c>code>6class="tags" href="/C.html" title=c>code> 的位置。

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">class="tags" href="/C.html" title=c>class="token operator">*class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(pulPtr class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">3class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token operator">+= class="tags" href="/C.html" title=c>class="token number">3class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>

这一行代码对数组中的一个元素进行了修改。首先࿰class="tags" href="/C.html" title=c>c;我们来看 <class="tags" href="/C.html" title=c>code>pulPtr + 3class="tags" href="/C.html" title=c>code>:

  • <class="tags" href="/C.html" title=c>code>pulPtrclass="tags" href="/C.html" title=c>code> 是指向数组第一个元素的指针。
  • <class="tags" href="/C.html" title=c>code>pulPtr + 3class="tags" href="/C.html" title=c>code> 表示将指针偏移到数组中第 4 个元素的地址(注意数组的索引是从 0 开始的)。

通过 <class="tags" href="/C.html" title=c>code>*(pulPtr + 3)class="tags" href="/C.html" title=c>code> 取出该地址所指向的值࿰class="tags" href="/C.html" title=c>c;也就是数组的第 4 个元素 <class="tags" href="/C.html" title=c>code>9class="tags" href="/C.html" title=c>code>。然后 <class="tags" href="/C.html" title=c>code>+= 3class="tags" href="/C.html" title=c>code> 操作将该值增加 3࿰class="tags" href="/C.html" title=c>c;变为 <class="tags" href="/C.html" title=c>code>12class="tags" href="/C.html" title=c>code>。因此࿰class="tags" href="/C.html" title=c>c;数组 <class="tags" href="/C.html" title=c>code>pulArrayclass="tags" href="/C.html" title=c>code> 现在的值变成了 <class="tags" href="/C.html" title=c>code>{6, 7, 8, 12, 10}class="tags" href="/C.html" title=c>code>。

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">printfclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token string">"%d, %d\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token operator">*pulPtrclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token operator">*class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(pulPtr class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token number">3class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>
  • <class="tags" href="/C.html" title=c>code>*pulPtrclass="tags" href="/C.html" title=c>code> 取出指针 <class="tags" href="/C.html" title=c>code>pulPtrclass="tags" href="/C.html" title=c>code> 所指向的值࿰class="tags" href="/C.html" title=c>c;即数组的第一个元素 <class="tags" href="/C.html" title=c>code>6class="tags" href="/C.html" title=c>code>。
  • <class="tags" href="/C.html" title=c>code>*(pulPtr + 3)class="tags" href="/C.html" title=c>code> 取出指向第 4 个元素的值࿰class="tags" href="/C.html" title=c>c;现在这个值已经被修改为 <class="tags" href="/C.html" title=c>code>12class="tags" href="/C.html" title=c>code>。

因此࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>printfclass="tags" href="/C.html" title=c>code> 最终输出的结果是:

<class="tags" href="/C.html" title=c>code>6, 12
class="tags" href="/C.html" title=c>code>

💯进一步优化和拓展

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />
为了更好地理解这个题目࿰class="tags" href="/C.html" title=c>c;我们来探讨一些与此相关的重要概念࿰class="tags" href="/C.html" title=c>c;包括指针运算、数组与指针的关系࿰class="tags" href="/C.html" title=c>c;以及 C 语言中的一些常见错误和陷阱。


1. 指针与数组的关系

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />
在 C 语言中࿰class="tags" href="/C.html" title=c>c;数组名在大多数情况下会退化为指向其首元素的指针。这意味着 <class="tags" href="/C.html" title=c>code>pulArrayclass="tags" href="/C.html" title=c>code> 可以被看作是指向 <class="tags" href="/C.html" title=c>code>pulArray[0]class="tags" href="/C.html" title=c>code> 的地址。因此࿰class="tags" href="/C.html" title=c>c;以下代码:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">pulPtr class="tags" href="/C.html" title=c>class="token operator">= pulArrayclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>code>

可以理解为让指针 <class="tags" href="/C.html" title=c>code>pulPtrclass="tags" href="/C.html" title=c>code> 指向数组的起始地址。此后࿰class="tags" href="/C.html" title=c>c;通过指针的偏移量我们可以访问数组中的其他元素࿰class="tags" href="/C.html" title=c>c;例如:

  • <class="tags" href="/C.html" title=c>code>pulPtr + 1class="tags" href="/C.html" title=c>code> 指向 <class="tags" href="/C.html" title=c>code>pulArray[1]class="tags" href="/C.html" title=c>code>
  • <class="tags" href="/C.html" title=c>code>pulPtr + 3class="tags" href="/C.html" title=c>code> 指向 <class="tags" href="/C.html" title=c>code>pulArray[3]class="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;即值为 <class="tags" href="/C.html" title=c>code>9class="tags" href="/C.html" title=c>code> 的位置

2. 指针运算的注意事项

指针的运算是基于指针所指向的数据类型大小的。例如࿰class="tags" href="/C.html" title=c>c;对于一个 <class="tags" href="/C.html" title=c>code>unsigned longclass="tags" href="/C.html" title=c>code> 类型的指针࿰class="tags" href="/C.html" title=c>c;每次加 <class="tags" href="/C.html" title=c>code>1class="tags" href="/C.html" title=c>code> 实际上会增加 <class="tags" href="/C.html" title=c>code>sizeof(unsigned long)class="tags" href="/C.html" title=c>code> 个字节࿰class="tags" href="/C.html" title=c>c;而不是简单地增加 1 个字节。这种特性在访问数组元素时尤为重要࿰class="tags" href="/C.html" title=c>c;因为它能保证指针准确地指向下一个元素࿰class="tags" href="/C.html" title=c>c;而不是指向任意的中间位置。

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />


3. 常见的错误和陷阱

  • 数组越界访问:在使用指针操作数组时࿰class="tags" href="/C.html" title=c>c;如果偏移量超出了数组的边界࿰class="tags" href="/C.html" title=c>c;就会发生越界访问࿰class="tags" href="/C.html" title=c>c;导致未定义行为。这种错误在 C 语言中非常常见且很难调试。
  • 未初始化指针:如果指针没有正确初始化(例如没有指向有效的内存地址)࿰class="tags" href="/C.html" title=c>c;对其解引用会导致严重的错误࿰class="tags" href="/C.html" title=c>c;例如段错误(<class="tags" href="/C.html" title=c>code>Segmentation Faultclass="tags" href="/C.html" title=c>code>)。在这道题中࿰class="tags" href="/C.html" title=c>c;指针 <class="tags" href="/C.html" title=c>code>pulPtrclass="tags" href="/C.html" title=c>code> 在赋值之前指向了有效的数组࿰class="tags" href="/C.html" title=c>c;因此没有这个问题。

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />


4. 拓展:指针操作的应用场景

指针的运算和数组的结合在许多场景中都非常有用࿰class="tags" href="/C.html" title=c>c;例如:

  • 遍历数组:通过指针偏移量来遍历数组中的元素࿰class="tags" href="/C.html" title=c>c;通常比使用数组索引更加高效࿰class="tags" href="/C.html" title=c>c;尤其是在嵌入式系统中࿰class="tags" href="/C.html" title=c>c;由于资源有限࿰class="tags" href="/C.html" title=c>c;高效地访问数据显得尤为重要。
  • 动态内存管理:在堆上分配内存时(例如使用 <class="tags" href="/C.html" title=c>code>malloclass="tags" href="/C.html" title=c>cclass="tags" href="/C.html" title=c>code>)࿰class="tags" href="/C.html" title=c>c;返回的是指向已分配内存的指针࿰class="tags" href="/C.html" title=c>c;之后对这块内存的操作大多需要通过指针进行。
  • 字符处理与字符串操作:指针在字符串处理方面非常高效࿰class="tags" href="/C.html" title=c>c;通过指针可以直接访问字符数组中的每一个字符࿰class="tags" href="/C.html" title=c>c;进行遍历、修改等操作。
    class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />

5. 指针与内存地址的深入理解

指针不仅仅是一个变量࿰class="tags" href="/C.html" title=c>c;它实际上是一个存储内存地址的变量。理解指针与内存地址之间的关系࿰class="tags" href="/C.html" title=c>c;对于掌握指针的高级用法至关重要。例如࿰class="tags" href="/C.html" title=c>c;指针的偏移量取决于它所指向的数据类型大小࿰class="tags" href="/C.html" title=c>c;这使得 C 语言中的指针非常灵活且强大。在涉及复杂数据结构(如链表、树结构)时࿰class="tags" href="/C.html" title=c>c;指针的使用必不可少࿰class="tags" href="/C.html" title=c>c;可以有效地管理和操纵动态数据。
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />


💯题目二

在另一段代码中࿰class="tags" href="/C.html" title=c>c;定义了一个自定义的字符串长度函数 <class="tags" href="/C.html" title=c>code>my_strlenclass="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;其目的是计算一个字符串的长度。

代码如下:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">my_strlenclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>charclass="tags" href="/C.html" title=c>class="token operator">* arrclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token operator">*arr class="tags" href="/C.html" title=c>class="token operator">!= class="tags" href="/C.html" title=c>class="token string">'\0'class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;arrclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">main3class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>char arrclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token string">"今天是2024年11月26日"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">printfclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token string">"%d"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">my_strlenclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(arrclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />


数组传递给函数时的特性

在 <class="tags" href="/C.html" title=c>code>main3()class="tags" href="/C.html" title=c>code> 函数中࿰class="tags" href="/C.html" title=c>c;定义了一个字符数组 <class="tags" href="/C.html" title=c>code>arrclass="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;并将其传递给了 <class="tags" href="/C.html" title=c>code>my_strlenclass="tags" href="/C.html" title=c>code> 函数。这个过程中࿰class="tags" href="/C.html" title=c>c;我们并不需要显式定义一个指针࿰class="tags" href="/C.html" title=c>c;原因在于 C 语言中的数组退化特性。
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />


数组退化为指针

  • 在调用 <class="tags" href="/C.html" title=c>code>my_strlen(arr)class="tags" href="/C.html" title=c>code> 时࿰class="tags" href="/C.html" title=c>c;数组名 <class="tags" href="/C.html" title=c>code>arrclass="tags" href="/C.html" title=c>code> 会自动退化为一个指向其首元素的指针。也就是说࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>my_strlenclass="tags" href="/C.html" title=c>code> 实际上接收的是 <class="tags" href="/C.html" title=c>code>arrclass="tags" href="/C.html" title=c>code> 的首地址(一个 <class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>char*class="tags" href="/C.html" title=c>code> 类型的指针)࿰class="tags" href="/C.html" title=c>c;这使得 <class="tags" href="/C.html" title=c>code>my_strlenclass="tags" href="/C.html" title=c>code> 可以遍历整个字符串直到遇到字符串的终止符 <class="tags" href="/C.html" title=c>code>\0class="tags" href="/C.html" title=c>code>。
  • 这种退化机制在 C 语言中被广泛使用࿰class="tags" href="/C.html" title=c>c;使得函数可以方便地操作数组数据࿰class="tags" href="/C.html" title=c>c;而不需要将整个数组拷贝传递࿰class="tags" href="/C.html" title=c>c;这样也提高了效率。
  • 此外࿰class="tags" href="/C.html" title=c>c;数组退化为指针的特性在很多场景下可以提高代码的通用性和复用性࿰class="tags" href="/C.html" title=c>c;因为指针可以指向不同的数组࿰class="tags" href="/C.html" title=c>c;从而实现对不同数据的操作。
    class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />

指针遍历字符串

在 <class="tags" href="/C.html" title=c>code>my_strlenclass="tags" href="/C.html" title=c>code> 函数中࿰class="tags" href="/C.html" title=c>c;使用指针 <class="tags" href="/C.html" title=c>code>arrclass="tags" href="/C.html" title=c>code> 来遍历整个字符串:

<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>c">class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token operator">*arr class="tags" href="/C.html" title=c>class="token operator">!= class="tags" href="/C.html" title=c>class="token string">'\0'class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;arrclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

这里࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>*arrclass="tags" href="/C.html" title=c>code> 取出当前字符的值࿰class="tags" href="/C.html" title=c>c;并判断是否为终止符 <class="tags" href="/C.html" title=c>code>\0class="tags" href="/C.html" title=c>code>。如果不是࿰class="tags" href="/C.html" title=c>c;就将 <class="tags" href="/C.html" title=c>code>arrclass="tags" href="/C.html" title=c>code> 向后移动一个字符位置࿰class="tags" href="/C.html" title=c>c;并增加计数器 <class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>code>。这种方式让函数能够有效地计算出字符串的长度࿰class="tags" href="/C.html" title=c>c;直到遇到字符串的结束符。
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />


输出结果

class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/9216f3aeclass="tags" href="/C.html" title=c>ce284327adf7b4b3class="tags" href="/C.html" title=c>cd8b838class="tags" href="/C.html" title=c>c.png" alt="在这里插入图片描述" />

在 <class="tags" href="/C.html" title=c>code>main3()class="tags" href="/C.html" title=c>code> 中࿰class="tags" href="/C.html" title=c>c;字符串为 <class="tags" href="/C.html" title=c>code>"今天是2024年11月26日"class="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>my_strlen(arr)class="tags" href="/C.html" title=c>code> 返回 <class="tags" href="/C.html" title=c>code>20class="tags" href="/C.html" title=c>code>࿰class="tags" href="/C.html" title=c>c;即字符串的长度。
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />


数组与指针的区别

  • 数组是连续内存块:<class="tags" href="/C.html" title=c>code>arrclass="tags" href="/C.html" title=c>code> 是一块连续的内存空间࿰class="tags" href="/C.html" title=c>c;包含所有字符。
  • 指针是地址变量:在传递给函数时࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>arrclass="tags" href="/C.html" title=c>code> 自动退化为指针࿰class="tags" href="/C.html" title=c>c;这个指针指向数组的首元素。通过指针操作࿰class="tags" href="/C.html" title=c>c;可以灵活地遍历或操作数组中的每一个元素。
  • 指针的灵活性:指针可以指向任意内存地址(只要是有效的内存)࿰class="tags" href="/C.html" title=c>c;这使得它在处理动态数据结构(如链表、树等)时特别有用。相比之下࿰class="tags" href="/C.html" title=c>c;数组的内存是固定的࿰class="tags" href="/C.html" title=c>c;数组名只能指向这一块特定的内存。
    class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />

指针与函数参数的灵活性

在许多情况下࿰class="tags" href="/C.html" title=c>c;使用指针来传递参数࿰class="tags" href="/C.html" title=c>c;可以更高效地操作数据。特别是在处理大型数据结构时࿰class="tags" href="/C.html" title=c>c;直接传递数组的首地址比拷贝整个数组更节省内存和时间。此外࿰class="tags" href="/C.html" title=c>c;通过指针࿰class="tags" href="/C.html" title=c>c;我们可以修改数组中的内容࿰class="tags" href="/C.html" title=c>c;而不仅仅是读取它们的值࿰class="tags" href="/C.html" title=c>c;这也是指针传递的一个重要优势。
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />


💯小结

  • class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/36faf3455d4b4a0b990743617f24aaed.png" alt="在这里插入图片描述" />
    在第一段代码中࿰class="tags" href="/C.html" title=c>c;定义指针 <class="tags" href="/C.html" title=c>code>pulPtrclass="tags" href="/C.html" title=c>code> 让我们可以通过指针偏移来访问和修改数组中的元素。这种操作在 C 语言中非常灵活和常用

  • 在第二段代码中࿰class="tags" href="/C.html" title=c>c;数组 <class="tags" href="/C.html" title=c>code>arrclass="tags" href="/C.html" title=c>code> 被传递给 <class="tags" href="/C.html" title=c>code>my_strlenclass="tags" href="/C.html" title=c>code> 函数࿰class="tags" href="/C.html" title=c>c;数组名自动退化为指针class="tags" href="/C.html" title=c>c;从而允许函数通过指针遍历整个字符串。这种机制使得字符串处理非常高效

  • 指针与数组C 语言中是非常重要且强大的工具class="tags" href="/C.html" title=c>c;能够有效地进行内存操作和<class="tags" href="/C.html" title=c>code>数据处理class="tags" href="/C.html" title=c>code>。在使用时需要小心指针的边界和初始化问题class="tags" href="/C.html" title=c>c;以避免常见的错误

  • 指针的灵活性使得它在许多场景下成为不可替代的工具class="tags" href="/C.html" title=c>c;尤其是在涉及复杂数据结构动态内存管理时。


class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />


class="tags" href="/C.html" title=c>class="blog-vote-box">

http://www.ppmy.cn/devtools/137592.html

相关文章

Wireshark常用功能使用说明

此处用于记录下本人所使用 wireshark 所可能用到的小技巧。Wireshark是一款强大的数据包分析工具&#xff0c;此处仅介绍常用功能。 Wireshark常用功能使用说明 1.相关介绍1.1.工具栏功能介绍1.1.1.时间戳/分组列表概况等设置 1.2.Windows抓包 2.wireshark过滤器规则2.1.wiresh…

godot游戏引擎_瓦片集和瓦片地图介绍

在 Godot 中&#xff0c;TileSet 和 TileMap 是用于处理瓦片地图的两个关键概念&#xff0c;它们的作用和用途有明显的区别。以下是两者的详细对比&#xff1a; 1. TileSet&#xff08;瓦片集&#xff09; TileSet 是资源&#xff0c;定义瓦片的内容和属性。 特点&#xff1a…

安全设备-日志审计-系统安装部署配置

3.1 系统安装部署概述 通过系统初始化安装部署&#xff0c;可实现对系统的基础管理工作。系统安装基本部署涉及功能有时间配置、 资产组、资产、用户组、用户、时间配置等&#xff09; 3.2 系统安装部署配置举例 3.2.1 用户场景 本阶段进行系统安装&#xff0c;进行相关设…

《全面解析图像平滑处理:多种滤波方法及应用实例》

简介&#xff1a;本文深入探讨图像平滑处理技术&#xff0c;针对图像中存在的噪声问题&#xff0c;详细介绍了均值滤波、方框滤波、高斯滤波、中值滤波、双边滤波和 2D 卷积&#xff08;自定义滤波&#xff09;等多种滤波方法。阐述了各滤波方法的原理差异&#xff0c;如均值滤…

hadoop-mapreduce词频统计

一、Map Reduce主要阶段 二、词频统计示例 0.MapReduce 词频统计(Word Count)示例图 1. Input 阶段&#xff08;输入阶段&#xff09; 输入数据是一段文本&#xff0c;如下&#xff1a; Hadoop is a big data framework. Hadoop can store vast data. Hadoop processes big …

【设计模式】创建型模式之单例模式(饿汉式 懒汉式 Golang实现)

定义 一个类只允许创建一个对象或实例&#xff0c;而且自行实例化并向整个系统提供该实例&#xff0c;这个类就是一个单例类&#xff0c;它提供全局访问的方法。这种设计模式叫单例设计模式&#xff0c;简称单例模式。 单例模式的要点&#xff1a; 某个类只能有一个实例必须…

模数转换芯片AD9215

AD9215 是 Analog Devices 公司推出的一款高性能、低功耗、单通道 10 位模数转换器(ADC)。它具有采样速率高达 65 MSPS 或 105 MSPS(不同型号),并广泛应用于通信、成像和仪器仪表等领域。 AD9215 的关键特性 分辨率: 10 位,适合高精度应用。采样速率: 两种型号: AD921…

win、linux等环境下python输出cpu、gpu、avx等硬件信息

测试时输出环境信息&#xff0c;方便判断是否可以开启部分优化 # 环境: 20241030 win10 vs2022 python3.9.13 # vs2022时&#xff0c;在 C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\Scripts 下运行 pip install import os import sys import psutil …