Call a function to exchange two numbers

Source: Internet
Author: User

/*

Time: May 27, 2013 23:28:43

Function: Call a function to exchange two numbers.

File Name: swap. cppc language cannot: Reference transfer)

*/

# Include "stdio. h"


// Value transfer

Void Exchg1 (int x, int y) // the x and y variables in the definition are called form parameters of the Exchg1 function.

{

Int tmp;

Tmp = x;

X = y;

Y = tmp;

}

// Address Transfer

Void Exchg2 (int * px, int * py)

{

Int tmp;

Tmp = * px;

* Px = * py;

* Py = tmp;

}

// Address transfer, but error.

Void Exchg3 (int * px, int * py)

{

Int * tmp;

Tmp = px;

Px = py;

Py = tmp;

}

// Reference Transmission

Void Exchg4 (int & x, int & y) // note that the format of the formal parameter at the definition is different from the value transfer.

{

Int tmp;

Tmp = x;

X = y;

Y = tmp;

}


Int main ()

{

Int a, B;

Printf ("enter two numbers: a and B \ n ");

Scanf ("% d", & a, & B );

Printf ("call the first function !!! \ N ");

Exchg1 (a, B );

Printf ("a = % d, B = % d \ n", a, B );

Printf ("call the second function !!! \ N ");

Exchg2 (& a, & B );

Printf ("a = % d, B = % d \ n", a, B );

Printf ("Call the third function !!! \ N "); // The original value is not exchanged.

Exchg3 (& a, & B );

Printf ("a = % d, B = % d \ n", a, B );

Printf ("Call the fourth function !!! \ N ");

Exchg4 (a, B); // The call method is the same as the value transfer method.

Printf ("a = % d, B = % d \ n", a, B );

// Note: For continuous calls, the value is changed again.

Return 0;

}


/*

1 *****************

To illustrate this problem, I will first provide a code:

Int a = 4;

Int x;

X =;

X = x + 3;

No. Now I ask you: what is the final a value and what is the x value?

A = 4 x = 7)

In this Code, you need to understand that although the value is assigned to x, the variable is not an x variable.

Any modification to x will not change the variable, although simple, and take it for granted.

The variable of the called function is released after it is called, so it will not change.

2 ***********************

Remote control changes the value of a and B. When the address of a is passed to pa

Changing the value of a also changes and is overwritten by the new value of * pa. Parameters and

Real parameters share the same storage space. If the values of elements in the parameter array change,

The values of elements in the real parameter array also change.

As shown in the following figure: Zhang San, xiao san, xiao san, you praised Zhang San and xiao san,

You praise John, and James will also be praised.

3 ***************

A and B are not affected because of the change of pa and pb address values.


This is my first technical log. If it is inappropriate, please criticize and correct it.

*/


This article is from the "exchange two numbers" blog, please be sure to keep this source http://5893947.blog.51cto.com/5883947/1269082

Related Article

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.