What exactly is the difference between “pass by reference” in C and in C++?

There are questions that already deal with the difference between passing by reference and passing by value. In essence, passing an argument by value to a function means that the function will have its own copy of the argument - itsvalue is copied. Modifying that copy will not modify the original object. However, when passing by reference, the parameter inside the functionrefers to the same object that was passed in - any changes inside the function will be seen outside.

Unfortunately, there are two ways in which the phrases "pass by value" and "pass by reference" are used which can cause confusion. I believe this is partly why pointers and references can be difficult for new C++ programmers to adopt, especially when they've come from a background in C.

C

In C, everything is passed by value in the technical sense. That is, whatever you give as an argument to a function, it will be copied into that function. For example, calling a functionvoid foo(int) with foo(x) copies the value of x as the parameter offoo. This can be seen in a simple example:

void foo(int param) { param++; }

int main()
{
  int x = 5;
  foo(x);
  printf("%d\n",x); // x == 5
}

The value of x is copied into foo and that copy is incremented. Thex in main continues to have its original value.

As I'm sure you're aware, objects can be of pointer type. For example, int* p definesp as a pointer to an int. It is important to note that the following code introduces two objects:

int x = 5;
int* p = &x;

The first is of type int and has the value 5. The second is of typeint* and its value is the address of the first object.

When passing a pointer to a function, you are still passing it by value. The address it contains is copied into the function. Modifying thatpointer inside the function will not change the pointer outside the function - however, modifyingthe object it points to will change the object outside the function. But why?

As two pointers that have the same value always point at the same object (they contain the same address), the object that is being pointed to may be accessed and modified through both. This gives the semantics of having passed the pointed to object by reference, although no references ever actually existed - there simply are no references in C. Take a look at the changed example:

void foo(int* param) { (*param)++; }

int main()
{
  int x = 5;
  foo(&x);
  printf("%d\n",x); // x == 6
}

We can say when passing the int* into a function, that the int it points to was "passed by reference" but in truth the int was never actually passed anywhere at all - only the pointer was copied into the function. This gives us the colloquial1 meaning of "pass by value" and "pass by reference".

The usage of this terminology is backed up by terms within the standard. When you have a pointer type, the type that it is pointing to is known as itsreferenced type. That is, the referenced type of int* is int.

A pointer type may be derived from a function type, an object type, or an incomplete  type, called thereferenced type.

While the unary * operator (as in *p) is known as indirection in the standard, it is commonly also known as dereferencing a pointer. This further promotes the notion of "passing by reference" in C.

C++

C++ adopted many of its original language features from C. Among them are pointers and so this colloquial form of "passing by reference" can still be used -*p is still dereferencing p. However, using the term will be confusing, because C++ introduces a feature that C doesn't have: the ability to truly passreferences.

A type followed by an ampersand is a reference type2. For example,int& is a reference to an int. when passing an argument to a function that takes reference type, the object is truly passed by reference. There are no pointers involved, no copying of objects, no nothing. The name inside the function actually refers to exactly the same object that was passed in. To contrast with the example above:

void foo(int& param) { param++; }

int main()
{
  int x = 5;
  foo(x);
  std::cout << x << std::endl; // x == 6
}

Now the foo function has a parameter that is a reference to an int. Now when passing x, param refers to precisely the same object. Incrementingparam has a visible change on the value of x and now x has the value 6.

In this example, nothing was passed by value. Nothing was copied. Unlike in C, where passing by reference was really just passing a pointer by value, in C++ we can genuinely pass by reference.

Because of this potential ambiguity in the term "pass by reference", it's best to only use it in the context of C++ when you are using a reference type. If you are passing a pointer, you are not passing by reference, you are passing a pointer by value (that is, of course,  unless you are passing a reference to a pointer! e.g. int*&). You may, however, come across uses of "pass by reference" when pointers are being used, but now at least you know what is really happening.


Other languages

Other programming languages further complicate things. In some, such as Java, every variable you have is known as a reference to an object (not the same as a reference in C++, more like a pointer), but those references are passed by value. So even though you appear to be passing to a function by reference, what you're actually doing is copying a reference into the function by value. This subtle difference to passing by reference in C++ is noticed when you assign a new object to the reference passed in:

public void foo(Bar param) {
  param.something();
  param = new Bar();
}

If you were to call this function in Java, passing in some object of type Bar, the call to param.something() would be called on the same object you passed in. This is because you passed in a reference to your object. However, even though a newBar is assigned to param, the object outside the function is still the same old object. The new one is never seen from the outside. That's because the reference insidefoo is being reassigned to a new object. This kind of reassigning references is impossible with C++ references.

设计一个函数:void find1(char array[], char search, char * pi)
要求:这个函数参数中的数组array是以0值为结束的字符串,要求在字符串array中查找字符是参数search里的字符。如果找到,函数通过第三个参数(pa)返回值为array字符串中第一个找到的字符的地址。如果没找到,则为pa为0。
设计:依题意,实现代码如下。

void find1(char [] array, char search, char * pa)
{
   int i;
   for (i=0;*(array+i)!=0;i++)
   {
      if (*(array+i)==search)
      {
        pa=array+i
        break;
      }
      else if (*(array+i)==0)
      {
        pa=0;
        break;
      }
   }
}

你觉得这个函数能实现所要求的功能吗?

调试:

我下面调用这个函数试试。

void main()
{
  char str[]={“afsdfsdfdf\0”};  //待查找的字符串
  char a=’d’;   //设置要查找的字符
  char * p=0;  //如果查找到后指针p将指向字符串中查找到的第一个字符的地址。
  find1(str,a,p);  //调用函数以实现所要操作。
  if (0==p )
  {
     printf (“没找到!\n”);//1.如果没找到则输出此句
  }
  else
  {
     printf(“找到了,p=%d”,p);  //如果找到则输出此句
  }
}

分析:

上面代码,你认为会是输出什么呢?
运行试试。
唉!怎么输出的是:没有找到!
而不是:找到了,……。
明明a值为’d’,而str字符串的第四个字符是’d’,应该找得到呀!
再看函数定义处:void find1(char [] array, char search, char * pa)
看调用处:find1(str,a,p);
依我在第五篇的分析方法,函数调用时会对每一个参数进行一个隐含的赋值操作。
整个调用如下:

   array=str;
   search=a;
   pa=p;    //请注意:以上三句是调用时隐含的动作。
   int i;
   for (i=0;*(array+i)!=0;i++)
   {
      if (*(array+i)==search)
      {
        pa=array+i
        break;
      }
      else if (*(array+i)==0)
      {
        pa=0;
        break;
      }
   }

哦!参数pa与参数search的传递并没有什么不同,都是值传递嘛(小语:地址传递其实就是地址值传递嘛)!所以对形参变量pa值(当然值是一个地址值)的修改并不会改变实参变量p值,因此p的值并没有改变(即p的指向并没有被改变)。

修正:

void find2(char [] array, char search, char ** ppa)
{
   int i;
   for (i=0;*(array+i)!=0;i++)
   {
      if (*(array+i)==search)
      {
        *ppa=array+i
        break;
      }
      else if (*(array+i)==0)
      {
        *ppa=0;
        break;
      }
   }
}

主函数的调用处改如下:
  find2(str,a,&p);  //调用函数以实现所要操作。

再分析:

这样调用函数时的整个操作变成如下:

   array=str;
   search=a;
   ppa=&p;    //请注意:以上三句是调用时隐含的动作。
   int i;
   for (i=0;*(array+i)!=0;i++)
   {
      if (*(array+i)==search)
      {
        *ppa=array+i
        break;
      }
      else if (*(array+i)==0)
      {
        *ppa=0;
        break;
      }
   }

看明白了吗?
ppa指向指针p的地址。
对*ppa的修改就是对p值的修改。
你自行去调试。
经过修改后的程序就可以完成所要的功能了

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值