通过引用调用和通过指针调用之间的差异 [英] Diff Between Call By Reference And Call By Pointer

查看:297
本文介绍了通过引用调用和通过指针调用之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我指针调用和引用调用之间的确切差异。



调用参考:

  void swap(int& x,int& y)
{
int temp;
temp = x; / *保存地址x * /
x = y的值; / * put y into x * /
y = temp; / * put x into y * /

return;
}

swap(a,b);

Call By Pointer:

  void swap(int * x,int * y)
{
int temp;
temp = * x; / *保存地址x * /
* x = * y的值; / * put y into x * /
* y = temp; / * put x into y * /

return;
}

swap(& a,& b);


解决方案

这两种情况完全一样。



但是,小的区别是,引用永远不是null (和内部函数,你确定,他们引用有效的变量)。另一方面,指针可以是空的,或者可以指向存储器中的无效位置,从而导致AV。


Can any one tell me the exact diff between call by pointer and call by reference. Actually what is happening on both case?

Eg:

Call By Reference:

void swap(int &x, int &y)
{
   int temp;
   temp = x; /* save the value at address x */
   x = y;    /* put y into x */
   y = temp; /* put x into y */

   return;
}

swap(a, b);

Call By Pointer:

void swap(int *x, int *y)
{
   int temp;
   temp = *x; /* save the value at address x */
   *x = *y; /* put y into x */
   *y = temp; /* put x into y */

   return;
}

  swap(&a, &b);

解决方案

The both cases do exactly the same.

However, the small difference is, that references are never null (and inside function you are sure, that they are referencing valid variable). On the other hand, pointers may be empty or may point to invalid place in memory, causing an AV.

这篇关于通过引用调用和通过指针调用之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆