#include <stdio.h>
#include<stdlib.h>
void swap1(int* a, int* b)
{
int* c = a;
a = b, b = c;
}
void swap2(int* a, int* b)
{
int c = *a;
*a = *b, * b = c;
}
int main()
{
int a = 10, b = 15;
swap1(&a, &b);
printf("%d,%d\n", a, b);
a = 10, b = 15;
swap2(&a, &b);
printf("%d,%d\n", a, b);
}
总结:
swap1传递一级指针,且操作一级指针不会对外部产生影响
swap2传递一级指针,操作0级指针,实现了交换