Using pointers in C language to implement two-digit Interchange (Code tutorial)

Source: Internet
Author: User

Using pointers in C language to implement two-digit Interchange (Code tutorial)
Using pointers in C language to implement two-digit swaps (Code tutorial)

/ *
Time: February 5, 2018 00:39:34
Title: C language two numbers interchange (using pointers)
Purpose: Understand the meaning of pointers and use them
* /
#include
int swap (int *, int *); // function forward declaration, you can omit parameters
int main (int argc, char * argv [])
{
int a = 1, b = 2;
printf ("a = 1, b = 2, please swap the values of a and b \ r \ n");
swap (& a, & b); // Outgoing variables of a and b: & a, & b
printf ("a, b values are swapped, \ na =% d, b =% d \ n", a, b);
return 0;
}
 
int swap (int * p, int * q) {// int * p, int * q declares the address variable p, q, which is used to receive the parameter & a, & b (not a , b)
int tmp;
tmp = * p; // * p is an int type
* p = * q;
* q = tmp;
}
/ *
Output results:
a = 1, b = 2, please exchange a and b values
After the a and b values are swapped,
a = 2, b = 1
 
* / 


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.