【C语言】传值调用与传址调用:深度解析与实现

devtools/2024/11/26 7:55:40/
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. 传值调用(Call by Value)
    • 2. 传址调用(Call by Referenclass="tags" href="/C.html" title=c>ce)
  • 💯传值调用与传址调用的区别
  • 💯深入理解指针与地址传递
  • 💯Java 中的传值与传址模拟
    • 1. Java 中的值传递
    • 2. Java 中通过对象实现交换
  • 💯传值调用和传址调用的应用场景
  • 💯传址调用中的风险和注意事项
  • 💯小结


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>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>原理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>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>。
    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/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" />


1. 传值调用(Call by Value)

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>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;原变量并不会受到任何影响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>副本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;可能会产生较高的内存和性能开销

  • 适用场景
    传值调用通常用于只需要读取数据而不对其进行修改的场合࿰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 maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><stdio.h>class="tags" href="/C.html" title=c>class="token keyword">void class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">swapclass="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 xclass="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 yclass="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 temp class="tags" href="/C.html" title=c>class="token operator">= xclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;x class="tags" href="/C.html" title=c>class="token operator">= yclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;y class="tags" href="/C.html" title=c>class="token operator">= tempclass="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">"Inside funclass="tags" href="/C.html" title=c>ction: x = %d, y = %d\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, xclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, yclass="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">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="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">int a class="tags" href="/C.html" title=c>class="token operator">= 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">, b class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">20class="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">"Before swap: a = %d, b = %d\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, bclass="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 funclass="tags" href="/C.html" title=c>ction">swapclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, bclass="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 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">"After swap: a = %d, b = %d\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, bclass="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>code>Before swap: a = 10, b = 20
Inside funclass="tags" href="/C.html" title=c>ction: x = 20, y = 10
After swap: a = 10, b = 20
class="tags" href="/C.html" title=c>code>

分析:
在上述示例中࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>swapclass="tags" href="/C.html" title=c>code> 函数中的 <class="tags" href="/C.html" title=c>code>xclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>yclass="tags" href="/C.html" title=c>code> 是 <class="tags" href="/C.html" title=c>code>aclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>bclass="tags" href="/C.html" title=c>code> 的副本࿰class="tags" href="/C.html" title=c>c;函数内部虽然交换了 <class="tags" href="/C.html" title=c>code>xclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>yclass="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>aclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>bclass="tags" href="/C.html" title=c>code>。因此࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>mainclass="tags" href="/C.html" title=c>code> 函数中的 <class="tags" href="/C.html" title=c>code>aclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>bclass="tags" href="/C.html" title=c>code> 的值在调用结束后并未改变。


class="tags" href="/C.html" title=c>ce_98">2. 传址调用(Call by Referenclass="tags" href="/C.html" title=c>ce)

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语言中࿰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>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;例如交换变量值动态修改外部数据结构的内容。

代码示例:

<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 maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><stdio.h>class="tags" href="/C.html" title=c>class="token keyword">void class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">swapclass="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 operator">*paclass="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 operator">*pbclass="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 tmp class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token operator">*paclass="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 class="tags" href="/C.html" title=c>comment">// 获取 a 的值class="tags" href="/C.html" title=c>class="token operator">*pa class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token operator">*pbclass="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 class="tags" href="/C.html" title=c>comment">// 将 b 的值赋给 aclass="tags" href="/C.html" title=c>class="token operator">*pb class="tags" href="/C.html" title=c>class="token operator">= tmpclass="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 class="tags" href="/C.html" title=c>comment">// 将 tmp(原来 a 的值)赋给 b
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">mainclass="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">int a class="tags" href="/C.html" title=c>class="token operator">= 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 keyword">int b class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">20class="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">"交换前: a=%d b=%d\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, bclass="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 funclass="tags" href="/C.html" title=c>ction">swapclass="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">&aclass="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">&bclass="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 class="tags" href="/C.html" title=c>comment">// 传递变量 a 和 b 的地址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">"交换后: a=%d b=%d\n"class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, bclass="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>code>交换前: a = 10, b = 20
交换后: a = 20, b = 10
class="tags" href="/C.html" title=c>code>

分析:
在这个例子中࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>swapclass="tags" href="/C.html" title=c>code> 函数通过指针 <class="tags" href="/C.html" title=c>code>paclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>pbclass="tags" href="/C.html" title=c>code> 接收到 <class="tags" href="/C.html" title=c>code>aclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>bclass="tags" href="/C.html" title=c>code> 的地址࿰class="tags" href="/C.html" title=c>c;使用解引用(<class="tags" href="/C.html" title=c>code>*paclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>*pbclass="tags" href="/C.html" title=c>code>)直接修改了 <class="tags" href="/C.html" title=c>code>aclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>bclass="tags" href="/C.html" title=c>code> 的值。因此࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>aclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>bclass="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>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;特别是对于复杂数据结构而言。

  • 传址调用
    通过直接传递地址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" />


💯深入理解指针与地址传递

在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;是C语言中强大的工具。
  • 解引用(Dereferenclass="tags" href="/C.html" title=c>cing):通过 <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;<class="tags" href="/C.html" title=c>code>int *paclass="tags" href="/C.html" title=c>code> 就是一个指向 <class="tags" href="/C.html" title=c>code>intclass="tags" href="/C.html" title=c>code> 类型变量的指针࿰class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>*paclass="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>malloclass="tags" href="/C.html" title=c>cclass="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>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" />


💯Java 中的传值与传址模拟

有的读者可能会问:“在其他编程语言中࿰class="tags" href="/C.html" title=c>c;这种传值传址的概念是如何体现的?”

例如࿰class="tags" href="/C.html" title=c>c;在 Java 中࿰class="tags" href="/C.html" title=c>c;所有参数传递都是值传递。但是࿰class="tags" href="/C.html" title=c>c;Java 的对象引用在表现上类似于**“传址调用”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="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" />


1. Java 中的值传递

对于基本数据类型࿰class="tags" href="/C.html" title=c>c;Java是值传递࿰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 class="tags" href="/C.html" title=c>class="prism language-java">class="tags" href="/C.html" title=c>class="token keyword">publiclass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>class class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>class-name">TestSwap 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">publiclass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">staticlass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">void class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">swapclass="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 xclass="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 yclass="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 temp class="tags" href="/C.html" title=c>class="token operator">= xclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;x class="tags" href="/C.html" title=c>class="token operator">= yclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;y class="tags" href="/C.html" title=c>class="token operator">= tempclass="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 class="tags" href="/C.html" title=c>class-name">Systemclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.outclass="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">printlnclass="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">"Inside funclass="tags" href="/C.html" title=c>ction: x = " class="tags" href="/C.html" title=c>class="token operator">+ x class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token string">", y = " class="tags" href="/C.html" title=c>class="token operator">+ yclass="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">publiclass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">staticlass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">void class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="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 class="tags" href="/C.html" title=c>class-name">Stringclass="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">] argsclass="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 a class="tags" href="/C.html" title=c>class="token operator">= 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 keyword">int b class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">20class="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 class="tags" href="/C.html" title=c>class-name">Systemclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.outclass="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">printlnclass="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">"Before swap: a = " class="tags" href="/C.html" title=c>class="token operator">+ a class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token string">", b = " class="tags" href="/C.html" title=c>class="token operator">+ bclass="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 funclass="tags" href="/C.html" title=c>ction">swapclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, bclass="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 class="tags" href="/C.html" title=c>class-name">Systemclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.outclass="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">printlnclass="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">"After swap: a = " class="tags" href="/C.html" title=c>class="token operator">+ a class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token string">", b = " class="tags" href="/C.html" title=c>class="token operator">+ bclass="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 punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

输出结果:

<class="tags" href="/C.html" title=c>code>Before swap: a = 10, b = 20
Inside funclass="tags" href="/C.html" title=c>ction: x = 20, y = 10
After swap: a = 10, b = 20
class="tags" href="/C.html" title=c>code>

在这个例子中࿰class="tags" href="/C.html" title=c>c;Java在调用 <class="tags" href="/C.html" title=c>code>swap(a, b)class="tags" href="/C.html" title=c>code> 时传递的也是 <class="tags" href="/C.html" title=c>code>aclass="tags" href="/C.html" title=c>code> 和 <class="tags" href="/C.html" title=c>code>bclass="tags" href="/C.html" title=c>code> 的副本࿰class="tags" href="/C.html" title=c>c;因此原始变量的值并未发生变化。


2. Java 中通过对象实现交换

Java可以通过传递对象来间接实现类似“传址调用”的效果࿰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 class="tags" href="/C.html" title=c>class="prism language-java">class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>class class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>class-name">SwapHelper 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 valueclass="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 class="tags" href="/C.html" title=c>class-name">SwapHelperclass="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 valueclass="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">thisclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.value class="tags" href="/C.html" title=c>class="token operator">= valueclass="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">publiclass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>class class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>class-name">TestSwap 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">publiclass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">staticlass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">void class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">swapclass="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 class="tags" href="/C.html" title=c>class-name">SwapHelper xclass="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 class="tags" href="/C.html" title=c>class-name">SwapHelper yclass="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 temp class="tags" href="/C.html" title=c>class="token operator">= xclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.valueclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;xclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.value class="tags" href="/C.html" title=c>class="token operator">= yclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.valueclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;yclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.value class="tags" href="/C.html" title=c>class="token operator">= tempclass="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">publiclass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">staticlass="tags" href="/C.html" title=c>c class="tags" href="/C.html" title=c>class="token keyword">void class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="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 class="tags" href="/C.html" title=c>class-name">Stringclass="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">] argsclass="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 class="tags" href="/C.html" title=c>class-name">SwapHelper a class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token keyword">new class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>class-name">SwapHelperclass="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 class="tags" href="/C.html" title=c>class-name">SwapHelper b class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token keyword">new class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>class-name">SwapHelperclass="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">20class="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 class="tags" href="/C.html" title=c>class-name">Systemclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.outclass="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">printlnclass="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">"Before swap: a = " class="tags" href="/C.html" title=c>class="token operator">+ aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.value class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token string">", b = " class="tags" href="/C.html" title=c>class="token operator">+ bclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.valueclass="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 funclass="tags" href="/C.html" title=c>ction">swapclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, bclass="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 class="tags" href="/C.html" title=c>class-name">Systemclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.outclass="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">printlnclass="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">"After swap: a = " class="tags" href="/C.html" title=c>class="token operator">+ aclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.value class="tags" href="/C.html" title=c>class="token operator">+ class="tags" href="/C.html" title=c>class="token string">", b = " class="tags" href="/C.html" title=c>class="token operator">+ bclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.valueclass="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 punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>

输出结果:

<class="tags" href="/C.html" title=c>code>Before swap: a = 10, b = 20
After swap: a = 20, b = 10
class="tags" href="/C.html" title=c>code>

这种方式通过对象封装变量࿰class="tags" href="/C.html" title=c>c;从而实现了交换的效果。虽然Java中没有像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" />

  1. 传值调用
    适用于不希望函数修改原始数据的场景࿰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;特别是在函数的输出和副作用需要被严格控制时。

  2. 传址调用
    适用于需要函数直接修改原始数据的场景࿰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://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>NULLclass="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>code>NULLclass="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>code>NULLclass="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>NULLclass="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="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/201f46e964414bb5a5e6class="tags" href="/C.html" title=c>c70363076db3.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" />
    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>code>参数的副本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>code>。在 Java 等其他语言中࿰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>基础概念class="tags" href="/C.html" title=c>code>对于编写健壮、高效的代码依然至关重要。
    对于 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>code>数据保护的需求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>code>࿰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="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/137063.html

相关文章

【C++篇】排队的艺术:用生活场景讲解优先级队列的实现

文章目录 须知 &#x1f4ac; 欢迎讨论&#xff1a;如果你在学习过程中有任何问题或想法&#xff0c;欢迎在评论区留言&#xff0c;我们一起交流学习。你的支持是我继续创作的动力&#xff01; &#x1f44d; 点赞、收藏与分享&#xff1a;觉得这篇文章对你有帮助吗&#xff1…

vue3.5

响应式属性解构 import { watchEffect } from vueconst { count } defineProps([count])watchEffect(() > {// 每次父组件中的 count 属性变化时都会触发这个日志console.log(count) }) useTemplateRef 获取 dom // MyComposable 封装的方法 获取dom export const useMy…

【GIT】TortoiseGit的变基(Rebase)操作

在 TortoiseGit 中&#xff0c;变基&#xff08;Rebase&#xff09; 是一个 Git 的高级操作&#xff0c;用来重新整理分支的提交记录&#xff0c;使它们的历史更加线性化。 1. 什么是变基&#xff1f; 变基的主要作用是将一个分支上的提交“移动”到另一个分支的末尾&#xff…

观察者模式和发布订阅模式

文章目录 手写观察者模式手写发布订阅模式 观察者模式 Subject 和 Observer 直接绑定&#xff0c;中间无媒介。如点击事件&#xff0c;事件直接和按钮进行绑定。 发布订阅模式 Publisher 和 Observer 相互不认识&#xff0c;中间有媒介。如在 A 组件中绑定一个事件&#xff…

SpringBoot(三十六)SpringBoot使用sentinel自定义注解实现限流

前边我们学习了阿里的限流工具sentinel。她是有一个SentinelResource注解可以使用的&#xff0c;但是呢&#xff0c;使用SentinelResource注解需要链接sentinel控制台&#xff0c;在控制台中创建对应的规则。 再在对应的方法中使用SentinelResource注解来配置功能。 但是呢&…

python操作Elasticsearch

使用elasticsearch 6.x版本&#xff0c;操作es数据。 #! -*- coding:utf-8 -* import timefrom elasticsearch import Elasticsearch, helpersclass EstUtil:_instance Nonedef __new__(cls, *args, **kwargs):if not cls._instance:cls._instance super(EstUtil, cls).__ne…

【论文笔记】Number it: Temporal Grounding Videos like Flipping Manga

&#x1f34e;个人主页&#xff1a;小嗷犬的个人主页 &#x1f34a;个人网站&#xff1a;小嗷犬的技术小站 &#x1f96d;个人信条&#xff1a;为天地立心&#xff0c;为生民立命&#xff0c;为往圣继绝学&#xff0c;为万世开太平。 基本信息 标题: Number it: Temporal Grou…

STM32H7开发笔记(2)——H7外设之多路定时器中断

STM32H7开发笔记&#xff08;2&#xff09;——H7外设之多路定时器中断 文章目录 STM32H7开发笔记&#xff08;2&#xff09;——H7外设之多路定时器中断0.引言1.CubeMX配置2.软件编写 0.引言 本文PC端采用Win11STM32CubeMX4.1.0.0Keil5.24.2的配置&#xff0c;硬件使用STM32H…