A reference is often used as a function argument, making the variable name in the function a variable alias in the calling program. This method of passing parameters is referred to as passing by reference. Passing by reference allows the called function to access variables in the calling function. C + + added this feature is beyond the C language, C language can only be passed by value. Passing by value causes the called function to use a worthwhile copy of the calling program. Of course, the C language also allows you to circumvent the restrictions of passing by value, by the way the pointer passes.
Code:
#include <stdio.h> #include <iostream> using namespace std;
void TestAddress1 (int p, int& a, int b) {cout << "TestAddress1" << Endl; cout << "P Address:" << &p << ";
P Value: "<< p << Endl; cout << "A address:" << &a << ";
A value: "<< a << Endl;" cout << "b Address: << &b <<";
B Value: "<< b << Endl;
int* C = &b;
c-= 2; cout << "C Address:" << c << ";
C Value: "<< *c << Endl;}
void TestAddress2 (int* a, int b) {cout << "TestAddress2" << Endl; cout << "A address:" << &a << ";
A value: "<< *a << Endl; cout << "b Address: << &b <<";
B Value: "<< b << endl;}"
int main () {int* a = new int[5];
memset (A, 0, sizeof (int) * 5);
A[0] = 123;
int b = 2;
int c = 3;
cout << "Out a address:" << &a << Endl; CoUT << "Out B Address:" << &b << Endl;
cout << "Out C Address:" << &c << Endl;
cout << "-----------------" << Endl;
TestAddress1 (b, B, c);
cout << "-----------------" << Endl;
TestAddress2 (A, b); }