有两种方法可以调用函数:(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() 函数将 a 和 b 的地址传递给 add() 函数。a 和 b 的地址分配给指针变量 x 和 y。
现在关注 add() 函数中的语句 “z = *x + *y;”。请记住,x 存储 a 的地址。*x 和 *y 中的取消引用运算符分别获取 a 和 b 的值,因此 z 是 main() 函数中 a 和 b 的相加。
示例:使用按引用调用交换值
让我们更详细地了解按引用调用机制的工作原理,借助以下交换两个变量值的示例。
#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() 函数中的变量 a 和 b 分别分配了内存地址 100 和 200 的位置。当它们的地址被传递给 x 和 y(请记住它们是指针)时,swap() 函数中的变量 x、y 和 z 分别在地址 1000、2000 和 3000 处创建。
由于“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”的值现在被交换。
下图直观地演示了它是如何工作的——
混合按值调用和按引用调用
可以使用函数调用机制,该机制是“按值调用”和“按引用调用”的组合。它可以称为“混合调用机制”,其中某些参数按值传递,其他参数通过引用传递。
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
当函数需要执行内存级操作(例如控制外围设备、执行动态分配等)时,通过引用调用机制被广泛使用。