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

#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;
}

你可能感兴趣的:(指针)