zoukankan      html  css  js  c++  java
  • 传值引用和调用引用的区别

    只需要记住一句话:

    传值引用一般就是生成一个临时对象,而引用调用是调用参数本身。

    参照下面C语言代码理解:

    在 test.h文件里实现两个方法

    
    

     #include <stdio.h>

    /*交换两个数*/
    void exchange(int x,int y){
        int temp;
        temp = x;
        x = y;
        y = temp;
        printf("交换后第一个数:
    %d
    交换后第二个数:
    %d
    ",x,y);
    } 
    
    /*交换两个数的指针*/
    void exchangeAddress(int *x,int *y){
        int temp = *x;
        *x = *y;
        *y = temp;
        printf("交换后第一个数:
    %d
    交换后第二个数:
    %d
    ",*x,*y);
    } 

    在 test.c文件里调用这两个方法如下:

    #include <stdio.h>
    #include "test.h"
    
    int  main(){
        int a, b;
        printf("请输入a: 
    ");
        scanf("%d",&a);
        
        printf("请输入b: 
    ");
        scanf("%d",&b);
    
        exchange(a,b);
        printf("交换后:
     a=%d
     b=%d
    ",a,b) ;
        
        exchangeAddress(&a,&b);
        printf("交换地址:
     a=%d
     b=%d
    ",a,b) ;
    
    } 
        

    打印结果:

  • 相关阅读:
    漫游Kafka介绍章节简介
    poj 2309 BST 使用树阵lowbit
    华为-on练习--小写字符数的统计显示
    OpenMp高速分拣
    eclipse 于 Tomcat于 热部署 project
    2015第49周二
    2015第49周一
    2015第48周六
    2015第48周五
    2015第48周四
  • 原文地址:https://www.cnblogs.com/lovemargin/p/10562386.html
Copyright © 2011-2022 走看看