C语言值和引用
有两种方法可以将数据以C语言传递到函数中,即
按值调用和
通过引用调用。
让我们一一理解使用c语言进行的按值调用和按引用调用。
通过C中的值调用
在按值调用方法中,实际参数的值将复制到形式参数中。换句话说,可以说在按值调用方法中的函数调用中使用了变量的值。
在按值调用方法中,我们不能通过形式参数来修改实际参数的值。
按值调用时,由于实际参数的值已复制到形式参数中,因此为实际参数和形式参数分配了不同的内存。
实际参数是在函数调用中使用的参数,而形式参数是在函数定义中使用的参数。
下面的示例让我们尝试理解c语言中按值调用的概念:
#include<stdio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}
输出
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100
按值调用示例: 交换两个变量的值
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10
}
输出
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
在C中通过引用调用
在按引用调用中,变量的地址作为实际参数传递到函数调用中。
由于传递了实际参数的地址,因此可以通过更改形式参数来修改实际参数的值。
在通过引用进行调用时,形式参数和实际参数的内存分配都是相似的。该函数中的所有操作都对存储在实际参数地址处的值执行,修改后的值将存储在同一地址。
请参考以下示例进行调用。
#include<stdio.h>
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
输出
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
通过引用调用示例: 交换两个变量的值
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20, b = 10
}
输出
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
在c中按值调用和按引用调用之间的区别
值调用 |
引用调研 |
值的副本传递到函数中 |
将值的地址传递给函数 |
在函数内部进行的更改仅限于该函数。更改形式参数不会更改实际参数的值。 |
在函数内部进行的更改也将在函数外部进行验证。实际参数的值确实会通过更改形式参数来更改。 |
实际参数和形式参数在不同的内存位置创建 |
实际参数和形式参数在相同的内存位置创建 |