11.18 first pointer operation

Source: Internet
Author: User

This is a problem. The teacher asked us to write a simple exchange program and realized that three steps are different or two values can be exchanged. So I want to define a simple function outside the main function to implement the function call and then implement the function.

At the beginning, it was like this:

Swap (int A, int B)

{

A = a ^ B;

B = a ^ B;

A = a ^ B;

}

But after the call, I found that the values of A and B in the main function were not actually changed. It only used copies of the values in the main function. How can I solve this problem?

The solution is as follows:

I:

Main ()

{

Int A = 1, B = 2;

Swap (& A, & B );

}

Swap (int * a, int * B)

{

* A = * a ^ * B; // a simple example can be provided, for example, the difference between 0000 0001 and 0000 0010, or two values can be passed.

* B = * a ^ * B;

* A = * a ^ * B;

}

II:

It is also possible to use intermediate variables, but this is really old, we still need to strive for every day, and strive to write high-quality programs !!!

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.