C语言函数的传值与传地址:
#include <stdio.h>
//Call By Value
void swap_value(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
//Call By Pointer
void swap_pointer(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
//Call By Reference
void swap_reference(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void main()
{
int a, b;
a = 1, b = 2;
printf("Initialization : a = %d, b = %d\n", a, b);
//传值
a = 1, b = 2;
swap_value(a, b);
printf("Call by value : a = %d, b = %d\n", a, b);
//传地址
a = 1, b = 2;
swap_pointer(&a, &b);
printf("Call by pointer : a = %d, b = %d\n", a, b);
//传引用(C++新增,作用类似Call by pointer)
a = 1, b = 2;
swap_reference(a, b);
printf("Call by reference : a = %d, b = %d\n", a, b);
system("pause");
}