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 ;涉及到函数与变量的交互机制 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>原理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 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语言 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/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" />
1. 传值调用(Call by Value)
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 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 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>副本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 ;例如一些数据分析 、统计 或只进行数据输出 的场景。使用传值调用 可以确保代码的高可维护性 和数据安全性 。
代码示例:
<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 mac lass="tags" href="/C.html" title=c >c ro property">class="tags" href="/C.html" title=c >c lass="token direc lass="tags" href="/C.html" title=c >c tive-hash"># class="tags" href="/C.html" title=c >c lass="token direc lass="tags" href="/C.html" title=c >c tive keyword">inc lass="tags" href="/C.html" title=c >c lude class="tags" href="/C.html" title=c >c lass="token string"><stdio.h> class="tags" href="/C.html" title=c >c lass="token keyword">void class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">swap 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 xclass="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 yclass="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 temp class="tags" href="/C.html" title=c >c lass="token operator">= xclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; x class="tags" href="/C.html" title=c >c lass="token operator">= yclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; y class="tags" href="/C.html" title=c >c lass="token operator">= tempclass="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">"Inside func lass="tags" href="/C.html" title=c >c tion: x = %d, y = %d\n" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, xclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, yclass="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">int class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">main 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">int a class="tags" href="/C.html" title=c >c lass="token operator">= 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">, b class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">20 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">"Before swap: a = %d, b = %d\n" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, bclass="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 func lass="tags" href="/C.html" title=c >c tion">swap class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, bclass="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 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">"After swap: a = %d, b = %d\n" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, bclass="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>
输出结果:
<c lass="tags" href="/C.html" title=c >c ode>Before swap: a = 10, b = 20
Inside func lass="tags" href="/C.html" title=c >c tion: x = 20, y = 10
After swap: a = 10, b = 20
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>swapc lass="tags" href="/C.html" title=c >c ode> 函数中的 <c lass="tags" href="/C.html" title=c >c ode>xc lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>yc lass="tags" href="/C.html" title=c >c ode> 是 <c lass="tags" href="/C.html" title=c >c ode>ac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>bc 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>xc lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>yc 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>ac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>bc 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>mainc lass="tags" href="/C.html" title=c >c ode> 函数中的 <c lass="tags" href="/C.html" title=c >c ode>ac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>bc lass="tags" href="/C.html" title=c >c ode> 的值在调用结束后并未改变。
c lass="tags" href="/C.html" title=c >c e_98">2. 传址调用(Call by Referenc lass="tags" href="/C.html" title=c >c e)
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语言 中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 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 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 mac lass="tags" href="/C.html" title=c >c ro property">class="tags" href="/C.html" title=c >c lass="token direc lass="tags" href="/C.html" title=c >c tive-hash"># class="tags" href="/C.html" title=c >c lass="token direc lass="tags" href="/C.html" title=c >c tive keyword">inc lass="tags" href="/C.html" title=c >c lude class="tags" href="/C.html" title=c >c lass="token string"><stdio.h> class="tags" href="/C.html" title=c >c lass="token keyword">void class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">swap 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 operator">* paclass="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 operator">* pbclass="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 tmp class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token operator">* paclass="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 c lass="tags" href="/C.html" title=c >c omment">// 获取 a 的值 class="tags" href="/C.html" title=c >c lass="token operator">* pa class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token operator">* pbclass="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 c lass="tags" href="/C.html" title=c >c omment">// 将 b 的值赋给 a class="tags" href="/C.html" title=c >c lass="token operator">* pb class="tags" href="/C.html" title=c >c lass="token operator">= tmpclass="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 c lass="tags" href="/C.html" title=c >c omment">// 将 tmp(原来 a 的值)赋给 b
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">main 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">int a class="tags" href="/C.html" title=c >c lass="token operator">= 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 keyword">int b class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">20 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">"交换前: a=%d b=%d\n" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, bclass="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 func lass="tags" href="/C.html" title=c >c tion">swap 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">& aclass="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">& bclass="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 c lass="tags" href="/C.html" title=c >c omment">// 传递变量 a 和 b 的地址 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">"交换后: a=%d b=%d\n" class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, bclass="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>
输出结果:
<c lass="tags" href="/C.html" title=c >c ode>交换前: a = 10, b = 20
交换后: a = 20, b = 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 ode>swapc lass="tags" href="/C.html" title=c >c ode> 函数通过指针 <c lass="tags" href="/C.html" title=c >c ode>pac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>pbc lass="tags" href="/C.html" title=c >c ode> 接收到 <c lass="tags" href="/C.html" title=c >c ode>ac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>bc 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>*pac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>*pbc lass="tags" href="/C.html" title=c >c ode>)直接修改了 <c lass="tags" href="/C.html" title=c >c ode>ac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>bc 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>ac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>bc 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 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 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语言中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语言中强大的工具。 解引用(Dereferenc lass="tags" href="/C.html" title=c >c ing):通过 <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 lass="tags" href="/C.html" title=c >c ;<c lass="tags" href="/C.html" title=c >c ode>int *pac lass="tags" href="/C.html" title=c >c ode> 就是一个指向 <c lass="tags" href="/C.html" title=c >c ode>intc 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>*pac 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>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语言 中的作用极为重要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语言 成为一个**“贴近硬件”**的编程语言。
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" />
💯Java 中的传值与传址模拟
有的读者可能会问:“在其他编程语言中c lass="tags" href="/C.html" title=c >c ;这种传值 和传址 的概念是如何体现的?”
例如c lass="tags" href="/C.html" title=c >c ;在 Java 中c lass="tags" href="/C.html" title=c >c ;所有参数传递都是值传递 。但是c lass="tags" href="/C.html" title=c >c ;Java 的对象引用 在表现上类似于**“传址调用”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 ;可以对 对象的状态**进行修改。
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" />
1. Java 中的值传递
对于基本数据类型c lass="tags" href="/C.html" title=c >c ;Java是值传递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 c lass="tags" href="/C.html" title=c >c lass="prism language-java">class="tags" href="/C.html" title=c >c lass="token keyword">public lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c lass class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c lass-name">TestSwap 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">public lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">static lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">void class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">swap 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 xclass="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 yclass="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 temp class="tags" href="/C.html" title=c >c lass="token operator">= xclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; x class="tags" href="/C.html" title=c >c lass="token operator">= yclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; y class="tags" href="/C.html" title=c >c lass="token operator">= tempclass="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 c lass="tags" href="/C.html" title=c >c lass-name">System class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. outclass="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">println 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">"Inside func lass="tags" href="/C.html" title=c >c tion: x = " class="tags" href="/C.html" title=c >c lass="token operator">+ x class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token string">", y = " class="tags" href="/C.html" title=c >c lass="token operator">+ yclass="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">public lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">static lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">void class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">main 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 c lass="tags" href="/C.html" title=c >c lass-name">String 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">] argsclass="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 a class="tags" href="/C.html" title=c >c lass="token operator">= 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 keyword">int b class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token number">20 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 c lass="tags" href="/C.html" title=c >c lass-name">System class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. outclass="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">println 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">"Before swap: a = " class="tags" href="/C.html" title=c >c lass="token operator">+ a class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token string">", b = " class="tags" href="/C.html" title=c >c lass="token operator">+ bclass="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 func lass="tags" href="/C.html" title=c >c tion">swap class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, bclass="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 c lass="tags" href="/C.html" title=c >c lass-name">System class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. outclass="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">println 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">"After swap: a = " class="tags" href="/C.html" title=c >c lass="token operator">+ a class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token string">", b = " class="tags" href="/C.html" title=c >c lass="token operator">+ bclass="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 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>Before swap: a = 10, b = 20
Inside func lass="tags" href="/C.html" title=c >c tion: x = 20, y = 10
After swap: a = 10, b = 20
c lass="tags" href="/C.html" title=c >c ode>
在这个例子中c lass="tags" href="/C.html" title=c >c ;Java在调用 <c lass="tags" href="/C.html" title=c >c ode>swap(a, b)c lass="tags" href="/C.html" title=c >c ode> 时传递的也是 <c lass="tags" href="/C.html" title=c >c ode>ac lass="tags" href="/C.html" title=c >c ode> 和 <c lass="tags" href="/C.html" title=c >c ode>bc lass="tags" href="/C.html" title=c >c ode> 的副本c lass="tags" href="/C.html" title=c >c ;因此原始变量的值并未发生变化。
2. Java 中通过对象实现交换
Java可以通过传递对象来间接实现类似“传址调用”的效果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 c lass="tags" href="/C.html" title=c >c lass="prism language-java">class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c lass class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c lass-name">SwapHelper 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 valueclass="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 c lass="tags" href="/C.html" title=c >c lass-name">SwapHelper 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 valueclass="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">this class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. value class="tags" href="/C.html" title=c >c lass="token operator">= valueclass="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">public lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">c lass="tags" href="/C.html" title=c >c lass class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c lass-name">TestSwap 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">public lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">static lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">void class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">swap 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 c lass="tags" href="/C.html" title=c >c lass-name">SwapHelper xclass="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 c lass="tags" href="/C.html" title=c >c lass-name">SwapHelper yclass="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 temp class="tags" href="/C.html" title=c >c lass="token operator">= xclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. valueclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; xclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. value class="tags" href="/C.html" title=c >c lass="token operator">= yclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. valueclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">; yclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. value class="tags" href="/C.html" title=c >c lass="token operator">= tempclass="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">public lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">static lass="tags" href="/C.html" title=c >c class="tags" href="/C.html" title=c >c lass="token keyword">void class="tags" href="/C.html" title=c >c lass="token func lass="tags" href="/C.html" title=c >c tion">main 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 c lass="tags" href="/C.html" title=c >c lass-name">String 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">] argsclass="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 c lass="tags" href="/C.html" title=c >c lass-name">SwapHelper a class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token keyword">new class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c lass-name">SwapHelper 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 c lass="tags" href="/C.html" title=c >c lass-name">SwapHelper b class="tags" href="/C.html" title=c >c lass="token operator">= class="tags" href="/C.html" title=c >c lass="token keyword">new class="tags" href="/C.html" title=c >c lass="token c lass="tags" href="/C.html" title=c >c lass-name">SwapHelper 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">20 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 c lass="tags" href="/C.html" title=c >c lass-name">System class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. outclass="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">println 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">"Before swap: a = " class="tags" href="/C.html" title=c >c lass="token operator">+ aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. value class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token string">", b = " class="tags" href="/C.html" title=c >c lass="token operator">+ bclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. valueclass="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 func lass="tags" href="/C.html" title=c >c tion">swap class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">( aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">, bclass="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 c lass="tags" href="/C.html" title=c >c lass-name">System class="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. outclass="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">println 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">"After swap: a = " class="tags" href="/C.html" title=c >c lass="token operator">+ aclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. value class="tags" href="/C.html" title=c >c lass="token operator">+ class="tags" href="/C.html" title=c >c lass="token string">", b = " class="tags" href="/C.html" title=c >c lass="token operator">+ bclass="tags" href="/C.html" title=c >c lass="token punc lass="tags" href="/C.html" title=c >c tuation">. valueclass="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 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>Before swap: a = 10, b = 20
After swap: a = 20, b = 10
c lass="tags" href="/C.html" title=c >c ode>
这种方式通过对象封装变量c lass="tags" href="/C.html" title=c >c ;从而实现了交换的效果。虽然Java中没有像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>输出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 ;例如交换数据 、修改数组内容 或者动态调整数据结构 。传址调用的最大优势 在于其<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://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>NULLc 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 ode>NULLc 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 ;可以采用以下几种方法:
指针初始化 : 始终将指针初始化为一个有效的地址 或 <c lass="tags" href="/C.html" title=c >c ode>NULLc 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>NULLc 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 ;特别是在大型项目 中尤为重要。
💯小结
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/201f46e964414bb5a5e6c lass="tags" href="/C.html" title=c >c 70363076db3.png#pic lass="tags" href="/C.html" title=c >c _c lass="tags" href="/C.html" title=c >c enter" alt="在这里插入图片描述" /> 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 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 ode>灵活性c lass="tags" href="/C.html" title=c >c ode>。在 Java 等其他语言中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>基础概念c 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>关键的技能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 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 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 ode>的程序至关重要。
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" />