9.6 更改主调函数中的变量
有时需要在一个函数中更改其他函数的变量。
交换两个变量x和y的值
x = y;
y = x;
不起作用,y和x的值等于原始y的值。
temp = y;
y = x;
x = temp;
成功交换x和y的值。
交换值
/* swap1.c -- first attempt at a swapping function */
#include <stdio.h>
void interchange(int u, int v); /* declare function */
int main(void)
{
int x = 5, y = 10;
printf("Originally x = %d and y = %d.\n", x , y);
interchange(x, y);
printf("Now x = %d and y = %d.\n", x, y);
return 0;
}
void interchange(int u, int v) /* define function */
{
int temp;
temp = u;
u = v;
v = temp;
}
/* 输出: