在 C 中將指標傳遞給函式



在 C 中,指標是一個儲存另一個變數地址的變數。它充當對原始變數的引用。指標可以像傳遞任何其他引數一樣傳遞給函式。

C 中的函式可以透過兩種方式呼叫:

要按引用呼叫函式,您需要將其定義為接收呼叫函式中變數的指標。以下是您將用於按引用呼叫函式的語法

type function_name(type *var1, type *var2, ...)

當按引用呼叫函式時,會傳遞實際引數變數的指標,而不是它們的值。

將指標傳遞給函式的優點

將指標傳遞給函式有兩個優點

  • 它克服了按值傳遞的限制。對被呼叫函式內部值所做的更改會直接在指標中儲存的地址處完成。因此,我們可以從另一個作用域操作一個作用域中的變數。
  • 它還克服了函式只能返回一個表示式的限制。透過傳遞指標,函式處理的效果直接發生在地址處。其次,如果我們返回陣列結構體變數的指標,則可以返回多個值。

在本章中,我們將瞭解如何:

  • 將指標傳遞給 int 變數
  • 將指標傳遞給陣列
  • 將指標傳遞給結構體

將指標傳遞給函式的示例

讓我們定義一個函式add(),它接收兩個變數的引用。當呼叫此類函式時,我們傳遞實際引數的地址。讓我們從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

透過傳遞指標交換值

將指標傳遞給函式的最常被引用的應用之一是如何交換兩個變數的值。

以下函式接收要交換值的兩個變數的引用:

/* function definition to swap the values */
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;
}

示例

main() 函式有兩個變數ab;它們的地址作為引數傳遞給 swap() 函式。

#include <stdio.h>

int swap(int *x, int *y){
   int z;
   z = *x;   
   *x = *y; 
   *y = z; 
}

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

將陣列指標傳遞給函式

在 C 程式設計中,陣列的名稱充當陣列第一個元素的地址;換句話說,它成為指向陣列的指標。

示例

在此示例中,我們在 main() 中聲明瞭一個未初始化的陣列,並將它的指標與一個整數一起傳遞給一個函式。

在函式內部,陣列將填充傳遞整數的平方、立方和平方根。該函式返回此陣列的指標,使用該指標,可以在 main() 函式中訪問和列印值。

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

int arrfunction(int, float *);

int main(){

   int x = 100;
   float arr[3];

   arrfunction(x, arr);

   printf("Square of %d: %f\n", x, arr[0]);
   printf("Cube of %d: %f\n", x, arr[1]);
   printf("Square root of %d: %f\n", x, arr[2]);
   
   return 0;
}

int arrfunction(int x, float *arr){
   arr[0] = pow(x,2);
   arr[1] = pow(x, 3);
   arr[2] = pow(x, 0.5);
}

輸出

執行此程式碼時,將產生以下輸出:

Square of 100: 10000.000000
cube of 100: 1000000.000000
Square root of 100: 10.000000

將字串指標傳遞給函式

讓我們看看另一個示例,我們將字串指標傳遞給函式。

示例

在此程式中,兩個字串傳遞給 compare() 函式。C 中的字串是 char 資料型別的陣列。我們使用strlen() 函式來查詢字串的長度。

#include <stdio.h>

int compare(char *, char *);

int main(){

   char str1[] = "BAT";
   char str2[] = "BALL";
   int ret = compare(str1, str2);
   
   return 0;
}

int compare (char *x, char *y){

   int val;

   if (strlen(x) > strlen(y)){
      printf("Length of Str1 is greater than or equal to the length of Str2");
   }
   else{
      printf("Length of Str1 is less than the length of Str2");
   }
}

輸出

執行此程式碼時,將產生以下輸出:

Length of Str1 is less than the length of Str2

將結構體指標傳遞給函式

在 C 程式設計中,結構體是一種異構資料型別,包含不同資料型別的元素。讓我們看看如何將結構體指標傳遞給函式。

示例

在此示例中,在 main() 中聲明瞭一個結構體變數rectangle,並將它的地址傳遞給名為area()的使用者定義函式。當被呼叫時,area() 函式能夠使用間接運算子“”來使用變數的元素。它計算結果並將其分配給area元素 r→area。

#include <stdio.h>
#include <string.h>

struct rectangle{
   float len, brd;
   double area;
};

int area(struct rectangle *);

int main(){

   struct rectangle s;
   printf("Input length and breadth of a rectangle");
   scanf("%f %f", &s.len, &s.brd);
   area(&s);

   return 0;

}

int area(struct rectangle *r){

   r->area = (double)(r->len*r->brd);
   printf("Length: %f \n Breadth: %f \n Area: %lf\n", r->len, r->brd, r->area);
   
   return 0;
}

輸出

執行程式碼並檢查它的輸出:

Input length and breadth of a rectangle
10.5 20.5
Length: 10.500000 
Breadth: 20.500000 
Area: 215.250000

將指標傳遞給函式的概念的邏輯擴充套件導致傳遞聯合體指標,即多維陣列的指標,傳遞自引用結構體的指標等,所有這些在不同的應用領域(如複雜資料結構、硬體控制程式設計等)中都有重要的用途。

廣告