函数间参数的传递

来源:互联网 发布:淘宝福尔摩斯探案全集 编辑:程序博客网 时间:2024/06/01 08:17

1.函数传递(复制数据到另一地址中)

void Swap1(int c,int d)
{
 int temp;
 temp=c;
 c=d;
 d=temp;
}

int main()
{
 int a=10,b=20;
 Swap1(a,b);
 printf("%d %d\n",a,b);
 Swap2(&a,&b);
}

结果:10 20

在内存中开辟另一端空间存储c d 使c=a;d=b;当交换c d 的值时a b的并不改变。

2.地址传递
void  Swap2(int *p,int *q)
{
 int temp;
 temp=*p;
 *p=*q;
 *q=temp;
}

#if 0
void  Swap3(int *p,int *q)
{
 int *temp;
 *temp=*p;
 *p=*q;
 *q=*temp;
}
#endif
int main()
{
 int a=10,b=20;
 Swap2(&a,&b);
 printf("%d %d\n",a,b);

 return 0;
}
结果 20 10;

将a b的地址存到p和q中通过操作p q的内容改变a b的地址。

3.全局变量

 

 

0 0
原创粉丝点击