首页 > 代码库 > 使用指针交换两个整数的值

使用指针交换两个整数的值

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

int main()
{
int a = 10;
int b = 20;

printf("changed before:");
printf("a=%d\tb=%d\n", a, b);

change_1(a,b);
printf("changed 1 affter:");
printf("a=%d\tb=%d\n", a, b);

change_2(&a,&b);
printf("changed 2 affter:");
printf("a=%d\tb=%d\n", a, b);

return 0;
}

void change_1(int a, int b)
{
int c = a;
a = b;
b = c;
}

void change_2(int *a, int *b)
{
int c = *a;
*a = *b;
*b = c;
}