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
* /