Wednesday, 2018-11-14, 10:22 AM
Logged in as Guest | Group "Guests"Welcome Guest | RSS

Love Makes The World Go Round

Site Menu
Tag Board
Site Working Days

Blog

Main » 2011 » September » 26 » 【数据结构】f.cpp 变量的引用类型和非引用类型的区别
11:24 PM
【数据结构】f.cpp 变量的引用类型和非引用类型的区别
/* 
* Copyright TES@NPU 
* School of Automation InfoSec
* All rights reserved.
*
* FileName: c1.h
* Version: 1.0
* Update @ Sep. 26st 2011
 */

// f.cpp 变量的引用类型和非引用类型的区别
#include <stdio.h>

void fa(int a)  // 在函数中改变a,将不会带回主调函数(主调函数中的a仍是原值)
{
a = 5;
printf("在函数fa中:a = %d\n", a);
}

void fb(int &a)  // 由于a为引用类型,在函数中改变a,其值将带回主调函数
{
a = 5;
printf("在函数fb中:a = %d\n", a);
}

void main()
{
int n;
n = 1;
printf("在主程中,调用函数fa之前:n = %d\n", n);

fa(n);
printf("在主程中,调用函数fa之后,调用函数fb之前:n = %d\n", n);

fb(n);
printf("在主程中,调用函数fb之后:n = %d\n", n);
}

/*
----------
Output in VC++ 2008:
在主程中,调用函数fa之前:n = 1
在函数fa中:a = 5
在主程中,调用函数fa之后,调用函数
在函数fb中:a = 5
在主程中,调用函数fb之后:n = 5
*/

Category: 独自歌唱 | Views: 634 | Added by: tes1991 | Tags: Junior, Algorithms, Data Structure, Magic Q, Learning | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *:
Tags...
Tencent
Statistics

Total online: 1
Guests: 1
Users: 0

Copyright MyCorp © 2018