c++引用

  • 引用的本质---一个常量指针
#include<iostream>
using namespace std;
void crzaychange(int &a, int &b)
{
    int c;
    c = a;
    a = b;
    b = c;
}

void main()
{
    int a = 100;
    int b = 88;
    crzaychange(a,b);
    cout << "now a is:" <<a<< endl;
    system("pause");
}
  • 交换字符串指针指向的值
#include<iostream>
using namespace std;


void swapelements(char *&a,char *&b)
{
    char* c;
    c = a;
    a = b;
    b = c;
}

void main()
{
    char a[100] = "727 is ready to move ";
    char b[100] = "it's a long ong jurney to the other side";
    char *pa = a;
    char *pb = b;
    cout << "now a is:" << pa << endl;

    swapelements(pa, pb);
    cout << "now a is:" << pa << endl;

    system("pause");
}
#include<iostream>
using namespace std;

void swapelements(char *&a,char *&b)
{
    char* c;
    c = a;
    a = b;
    b = c;
}

void main()
{
    char *a= "727 is ready to move ";
    char *b = "it's a long ong jurney to the other side";
    
    cout << "now a is:" << a << endl;

    swapelements(a, b);
    cout << "now a is:" << a << endl;

    system("pause");
}

输出结果:

原文地址:https://www.cnblogs.com/saintdingspage/p/12057702.html