C語言中的引用呼叫



函式的呼叫方式有兩種:(a) 值傳遞和 (b) 引用傳遞。在本節中,我們將解釋引用傳遞的機制。

讓我們從簡要概述“指標”和“地址運算子(&)”開始本節。學習這兩個概念對於充分理解引用傳遞的機制非常重要。

C語言中的地址運算子(&)

在C語言中,變數是命名記憶體位置。當宣告變數時,編譯器會在記憶體中分配一個隨機位置,並在內部使用使用者定義的名稱來標識該位置。

要獲取變數建立的地址,我們使用地址(&)運算子

示例

請看下面的例子 -

#include <stdio.h>

int main(){

   int x = 10;
    
   printf("x: %d Address of x: %d", x, &x);
}

輸出

這將列印x的值及其地址 -

x: 10 Address of x: -1990957196

什麼是C語言中的指標?

指標是一個儲存另一個變數地址的變數。要宣告指標變數,其名稱字首為*符號。指標變數和其宿主變數的型別必須相同。

地址由&運算子分配。解引用運算子(*)與指標一起使用。它獲取地址分配給指標的變數的值。

示例

下面的例子演示了C語言中引用和解引用的工作方式 -

#include <stdio.h>

int main(){

   int x = 10;
   int *y = &x;

   printf("x: %d Address of x: %d\n", x, &x);
   printf("Address of y: %d \n", &y);
   printf("Value at address in y: %d\n", *y);
}

輸出

執行程式碼並檢查其輸出 -

x: 10 Address of x: -1742755108
Address of y: -1742755104 
Value at address in y: 10

C語言中的引用傳遞是如何工作的?

當函式被引用呼叫時,傳遞的是實際引數變數的地址,而不是它們的值。

讓我們定義接收兩個變數引用的add()函式 -

int add(int *x, int *y){

   int z = *x + *y;

   return z;
}

當呼叫這樣的函式時,我們傳遞實際引數的地址。

示例

讓我們從main()函式內部透過引用呼叫add()函式 -

#include <stdio.h>

/* function declaration */
int add(int *, int *);

int main(){

   int a = 10, b = 20;
   int c = add(&a, &b);
   
   printf("Addition: %d", c);
}

int add(int *x, int *y){
   
   int z = *x + *y;
   
   return z;
}

輸出

當您執行此程式碼時,它將產生以下輸出 -

Addition: 30

現在讓我們瞭解這段程式碼是如何工作的。main()函式將ab的地址傳遞給add()函式。ab的地址被分配給指標變數xy

現在關注add()函式內部的語句“z = *x + *y;”。請記住,x儲存a的地址。*x*y中的解引用運算子分別獲取ab的值,因此z是main()函式中ab的和。

示例:使用引用傳遞交換值

讓我們藉助以下交換兩個變數值的示例,更詳細地瞭解引用傳遞機制的工作原理。

#include <stdio.h>

/* Function definition to swap the values */
/* It receives the reference of two variables whose values are to be swapped */

int swap(int *x, int *y){

   int z;

   z = *x;   /* save the value at address x */
   *x = *y;  /* put y into x */
   *y = z;   /* put z into y */
  
   return 0;
}

/* The main() function has two variables "a" and "b" */ 
/* Their addresses are passed as arguments to the swap() function. */

int main(){

   /* local variable definition */
   int a = 10;
   int b = 20;
 
   printf("Before swap, value of a: %d\n", a );
   printf("Before swap, value of b: %d\n", b );
 
   /* calling a function to swap the values */
   swap(&a, &b);
 
   printf("After swap, value of a: %d\n", a);
   printf("After swap, value of b: %d\n", b);
 
   return 0;
}

輸出

當您執行此程式碼時,它將產生以下輸出 -

Before swap, value of a: 10
Before swap, value of b: 20
After swap, value of a: 20
After swap, value of b: 10

解釋

假設main()函式中的變數ab分別分配了記憶體地址100和200。由於它們的地址被傳遞給xy(請記住它們是指標),因此swap()函式中的變數xyz分別在地址1000、2000和3000處建立。

Inside the swap in C

由於“x”和“y”儲存“a”和“b”的地址,“x”變為100,“y”變為200,如上圖所示。

在swap()函式內部,第一條語句“z = *x”導致“x”中地址處的值儲存在“x”(即10)中。類似地,在語句“*x = *y;”中,將“y”中地址處的值(即20)儲存在指標為“x”的位置中。

最後,語句“*y = z;”將“z”賦值給“y”指向的變數,即main()函式中的“b”。現在,“a”和“b”的值被交換了。

下圖直觀地演示了它是如何工作的 -

Understand Visually how this works in C

混合使用值傳遞和引用傳遞

您可以使用值傳遞和引用傳遞的組合作為函式呼叫機制。它可以被稱為“混合呼叫機制”,其中一些引數透過值傳遞,其他引數透過引用傳遞。

C語言中的函式可以有多個引數,但只能返回一個值。引用傳遞機制是克服此限制的一個好方法。

示例

在此示例中,calculate()函式透過值接收整數引數,並接收兩個指標,其中儲存其平方和立方。

#include <stdio.h>
#include <math.h>

/* function declaration */
int calculate(int, int *, int *);

int main(){

   int a = 10;
   int b, c;

   calculate(a, &b, &c);

   printf("a: %d \nSquare of a: %d \nCube of a: %d", a, b, c);
}

int calculate(int x, int *y, int *z){

   *y  = pow(x,2);
   *z = pow(x, 3);

   return 0;
}

輸出

當您執行此程式碼時,它將產生以下輸出 -

a: 10 
Square of a: 100 
Cube of a: 1000

當函式需要執行記憶體級操作(例如控制外圍裝置、執行動態分配等)時,廣泛使用引用傳遞機制。

廣告