引用传值和指针传值的区别_按引用致电和按值致电之间的区别| 指针的使用

引用传值和指针传值的区别

If we consider the main use of pointer, then it is,

如果我们考虑使用指针的主要用途,那就是

  1. Dynamic memory allocation

    动态内存分配

  2. Call by reference

    通过参考电话

按值调用和按引用调用的区别 (Difference of call by value and call by reference)

  1. Call by value

    按价值致电

  2. Call by reference

    通过参考电话

First, go to through the code, run it and follow the output.

首先,遍历代码,运行并遵循输出。

Code:

码:

#include <stdio.h>
#include <unistd.h>

//function for call by value
void swap_call_by_value(int a, int b)
{

    //normal swap operation

    int temp;
    temp = a;
    a = b; //a now have value of b
    b = temp; //b now have value of a
    printf("Printing with in the function call by value swap...\n");
    printf("value of a afrer swap(call by value) %d \n", a);
    printf("value of b afrer swap(call by value) %d \n", b);
}

//function for call by reference
void swap_call_by_reference(int* a, int* b)
{

    //follow the parameter
    //a and b is now pointr type variables

    //normal swap operation

    int temp;
    temp = *a; //temp stores value at address &a
    *a = *b; //value at address &a is now value of address &b
    *b = temp; //value at address &b is now temp
    printf("Printing within the function call by reference swap...\n");
    printf("value of a afrer swap(call by reference) %d \n", *a);
    printf("value of b afrer swap(call by reference) %d \n", *b);
}

int main()
{

    int a = 10, b = 20;
    printf("Let's swap using call by value\n");
    sleep(2);
    swap_call_by_value(a, b);
    printf("the above function should have swapped already, \n");
    printf(" as it shows result printed with in function\n");
    sleep(2);
    printf("But is it swapped?\n");
    printf("Let's check by Printing here...\n");
    sleep(2);
    printf("value of a after swap(call by value) %d \n", a);
    printf("value of b after swap(call by value) %d \n", b);

    printf("you can see value is same as before, no swap\n");
    sleep(2);
    printf("What happened then?\n");
    sleep(1);
    printf("But before lets swap perfectly\n");

    printf("swapping by call by reference");
    sleep(2);

    //call by reference, address/reference is passed, not the value
    swap_call_by_reference(&a, &b);
    sleep(2);
    printf("the above function should have swapped already, \n");
    printf("as it shows result printed with in function\n");

    printf("But is it swapped?\n");
    printf("Let's check by Printing here...\n");
    sleep(2);
    printf("value of a after swap(call by reference) %d \n", a);
    printf("value of b after swap(call by reference) %d \n", b);

    printf("you can see now value is swapped...\n");

    return 0;
}

Note: The sleep() function used in code is only for display purpose, no relation with code.

注意:代码中使用的sleep()函数仅用于显示目的,与代码无关。

Output

输出量

Let's swap using call by value
Printing with in the function call by value swap... 
value of a afrer swap(call by value) 20 
value of b afrer swap(call by value) 10 
the above function should have swapped already, 
 as it shows result printed with in function
But is it swapped?
Let's check by Printing here... 
value of a after swap(call by value) 10 
value of b after swap(call by value) 20 
you can see value is same as before, no swap
What happened then? 
But before lets swap perfectly
swapping by call by referencePrinting within the function call by reference swap... 
value of a afrer swap(call by reference) 20 
value of b afrer swap(call by reference) 10 
the above function should have swapped already, 
as it shows result printed with in function 
But is it swapped?
Let's check by Printing here... 
value of a after swap(call by reference) 20 
value of b after swap(call by reference) 10 
you can see now value is swapped...

Explanation:

说明:

In case of call by value:

如果按值调用:

You can see that within the function it looked like it has swapped successfully, but when printed in the main function we can see values have not been swapped. The reason is the swapping took place within the local variables of the function.

您可以看到在函数中看起来好像已经成功交换了,但是在主函数中打印时,我们可以看到值尚未交换。 原因是交换发生在函数的局部变量内。

Our function call in the main function: swap_call_by_value(a,b);

我们在主函数中调用函数: swap_call_by_value(a,b);

Here a, b is the variable defined in main function

这里a,b是主函数中定义的变量

But in the function definition,

但是在函数定义中

    void swap_call_by_value(int a, int b){
        ...
    } 

Here a, b is different, these are local variables of the above function.

这里a , b不同,它们是上述函数的局部变量。

Say these two are local_a, local_b

说这两个是local_a , local_b

And a, b of the main function are main_a, main_b.

主函数的a , b是main_a , main_b 。

Addresses of local_a, local_b is different from the main_a, main_b.

local_a的地址,local_b从main_a,main_b不同。

Hence, the swap operation in the function is also local, the effect not reflected in main function, but working fine in the function itself.

因此,函数中的交换操作也是局部的,效果未在主函数中反映出来,但在函数本身中可以正常工作。

While calling a function, the function saves its local variable in its own activation record. Thus, it can't update the variable of main.

调用函数时,函数将其局部变量保存在其自己的激活记录中。 因此,它无法更新main的变量。

In case of call by reference

在通过参考致电的情况下

You are sending addresses of your variables in the main function swap_call_by_value(&a,&b);

您正在主函数swap_call_by_value(&a,&b);中发送变量的地址。

Hence the swapping is taking place on the same variable defined in the main function, thus, it's reflected even in the main function.

因此,交换发生在主函数中定义的同一变量上,因此,即使在主函数中也反映了这一点。

That's why to swap by function always use call by reference, and on other cases, you can use call by reference (use of pointer) where you need to reflect the effect of function on the variables declared in main functions.

这就是为什么按函数交换总是使用按引用调用,在其他情况下,可以在需要反映函数对主函数中声明的变量的影响时使用按引用调用(使用指针)。

翻译自: https://www.includehelp.com/c/call-by-reference-and-call-by-value.aspx

引用传值和指针传值的区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值