Use reference as Function argument __ function

Source: Internet
Author: User

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); }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.