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

news/2024/11/26 18:22:05/
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/news/1550115.html

相关文章

故障排除-------K8s挂载集群外NFS异常

故障排除-------K8s挂载集群外NFS异常 1. 故障现象2. 原因梳理2.1 排查思路2.2 确认yaml内容2.3 创建k8s内的nfs测试2.3.1 创建nfs和svc2.3.2 测试创建pvc2.3.3 测试结果 2.4 NFS服务端故障排除2.4.1 网络阻断排除2.4.2 排除服务状态问题2.4.3 排查NFS权限问题 3. 故障排除 1. …

生成式AI在教育技术中的应用:变革与创新

引言 在过去的几十年中&#xff0c;人工智能技术在教育领域的应用引发了显著的变革&#xff0c;从早期的简单自动化评估系统到现在的个性化学习助手。生成式AI&#xff08;AIGC&#xff09;作为AI领域的重要分支&#xff0c;正在快速进入教育技术的主流&#xff0c;为教育带来…

安全加固方案

交换机安全加固 查看是否关闭未使用的接口 25GE1/0/1、25GE1/0/47、25GE1/0/48需要使用&#xff0c;暂不关闭 system-view # interface Eth-Trunk99 shutdown quit interface Eth-Trunk100 shutdown quit interface Eth-Trunk110 shutdown quit interface 25GE1/…

反向代理服务器的用途

代理服务器在网络中扮演着重要的角色&#xff0c;它们可以优化流量、保护服务器以及提高安全性。在代理服务器中&#xff0c;反向代理服务器是一种特殊类型&#xff0c;常用于Web服务器前&#xff0c;它具备多种功能&#xff0c;能够确保网络流量的顺畅传输。那么&#xff0c;让…

Flink学习连载第二篇-使用flink编写WordCount(多种情况演示)

使用Flink编写代码&#xff0c;步骤非常固定&#xff0c;大概分为以下几步&#xff0c;只要牢牢抓住步骤&#xff0c;基本轻松拿下&#xff1a; 1. env-准备环境 2. source-加载数据 3. transformation-数据处理转换 4. sink-数据输出 5. execute-执行 DataStream API开发 //n…

CentOS 7安装SSHFS 实现远程主机目录 挂载为本地目录

安装sshfs 官方下载地址 https://github.com/libfuse/sshfs/releases 首先&#xff0c;我们需要安装sshfs软件。sshfs是一个基于SSH文件传输协议的文件系统客户端&#xff0c;它的官方网页是&#xff1a;http://fuse.sourceforge.net/sshfs.html 。在CentOS下&#xff0c;我们…

m个数 生成n个数的所有组合 详解

要从给定的 m 个数 中生成 n 个数的所有组合&#xff0c;我们可以使用递归或迭代方法&#xff0c;具体解决过程如下&#xff1a; 1. 问题说明 给定一个大小为 m 的数组&#xff0c;例如 [1, 2, 3]&#xff0c;生成所有长度为 n 的组合&#xff08;可以包括重复数字&#xff0c…

深度学习day4-模型

八 手动构建模型实战 1 构建数据集 epoch&#xff1a;使用训练集的全部数据对模型进行一次完整的训练&#xff0c;被称为一代训练 batch&#xff1a;使用训练集中的部分样本对模型权重进行一次反向传播声望参数更新&#xff0c;这部分样本被称为一批数据 iteration:使用一个…