传递指针和传递引用

[flydream@flydream ThinkingInC++]$ cat PassReference.cpp
#include <iostream>

using namespace std;

void passPointer(int *P)
{
    cout << "P = " << P << endl;
    cout << "*P = " << *P << endl;
    *P = 100;    
}

void passReference(int &R)
{
    cout << "R = " << R << endl;
    cout << "&R = " << &R << endl;
    R = 200;
}

int main(int argc, char **argv)
{
    int P = 10, R = 20;
    cout <<"======================= PASS POINTER ===================" << endl;    
    cout << "P = " << P <<endl;
    cout << "&P = " << &P <<endl;
    passPointer(&P);
    cout << "execute Function later : P = " << P << endl;
    
    cout <<"======================= PASS REFERENCE ===================" << endl;    
    cout << "R = " << R <<endl;
    cout << "&R = " << &R <<endl;
    passReference(R);
    cout << "execute Function later : R = " << R << endl;
    
    return 0;
        
}
[flydream@flydream ThinkingInC++]$ g++ PassReference.cpp
[flydream@flydream ThinkingInC++]$ ./a.out
======================= PASS POINTER ===================
P = 10
&P = 0xbfb2ed0c
P = 0xbfb2ed0c
*P = 10
execute Function later : P = 100
======================= PASS REFERENCE ===================
R = 20
&R = 0xbfb2ed08
R = 20
&R = 0xbfb2ed08
execute Function later : R = 200
[flydream@flydream ThinkingInC++]$


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值