指针与函数参数
被调用函数不能直接修改主调函数中变量的值,但通过传递指针即可实现。
#include <stdio.h>
void swap1(int x, int y)
{
int tmp;
tmp = x;
x = y;
y = tmp;
}
void swap2(int *x, int *y)
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
int main(int argc, char const *argv[])
{
int i1 = 233;
int i2 = 666;
swap1(i1, i2);
printf("i1 = %d, i2 = %d.\n", i1, i2);
swap2(&i1, &i2);
printf("i1 = %d, i2 = %d.\n", i1, i2);
return 0;
}