# C语言 第 11节 指针有TM 什么用 - 1

70后说80后太狂，80后说90后太傻，90后说自己是傻B；

#include <stdio.h>
void exchange(int a, int b);

int main(void)
{
int x = 10;
int y = 5;
printf("old x = %d and old y = %d\n", x, y);
exchange(x, y);  // 只是将　x 和 y 的值传递给　a　和　ｂ；并没有改变　ｘ　和　ｙ本身的值（最后打印的值可以看出来）
printf("old x = %d and old y = %d\n", x, y);
return 0;
}

void exchange(int a, int b)
{
int temp;
printf("old a = %d and old b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("new a = %d and new b = %d\n", a, b);
}

old x = 10 and old y = 5
old a = 10 and old b = 5
new a = 5 and new b = 10
old x = 10 and old y = 5

#include <stdio.h>
void exchange(int *a, int *b);

int main(void)
{
int x = 10;
int y = 5;
printf("old x = %d and old y = %d\n", x, y);
exchange(&x, &y);   //与上面的一个函数的区别在于这里传递的是 x 和 y 的地址 被调函数是对地址中所存储的值进行操作 地址不变 变的的是存储的值
printf("old x = %d and old y = %d\n", x, y);
return 0;
}

void exchange(int *a, int *b) // * 这个符号到底是啥意思  书中明确写的这个符号叫 间接运算符； *a 表示指向a地址所存储的值
{
int temp;
printf("old *a = %d and old *b = %d\n", *a, *b);
temp = *a;
*a =　*b;
*b =　temp;
printf("new *a = %d and new *b = %d\n", *a, *b);
}

old x = 10 and old y = 5
old *a = 10 and old *b = 5
new *a = 5 and new *b = 10
old x = 5 and old y = 10