Arrays passed by reference by default?

You're right. The wording is very confusing and uses a meaning of "reference" that is not the same as the term reference relating to the C++ feature of the same name. Instead, it's talking about the way that array names decay to pointers — in fact you do not "pass an array" like this at all!

In the "olden days" "reference" was used in a more general sense in the same way as "handle" — an abstract term to represent the use of indirection to fake by-reference semantics in languages that did not support it. But, C++ does support things that it calls references; thus, we tend not to use "reference" in its "handle" sense when talking about C++ (where Deitel ∉ "we", evidently).

Recommended reading:

  • http://jcatki.no-ip.org/fncpp/Resources
  • The Definitive C++ Book Guide and List

Any other C++ book be very wary!! Though in the majority of areas of life it would be insane of me to suggest that inclusion in the above two specific lists is a definitive pre-requisite for a book to be considered "good", there is a sufficient wealth of dangerously incorrect C++ text out there (such as the text you quote) and this is a sufficiently big problem for our language newcomers that in the world of C++ books it's actually a good rule of thumb to follow.


To summarise my comment, you are absolutely right!

The book is wrong in its choice of jargon. It tries to talk about arrays decaying to C pointers. It refers to passing this pointer by value as passing by reference which is WRONG.

This is a common enough misconception, I believe I was taught this way as well (passing pointer as value == passing by reference). This is completely wrong in terms of C++ references and pass by reference.

If this were correct, I wouldn't be able to do this..

void ModifyMyArray(int *array){
   int oops[4]= {0};
   array = oops;

   array[2] = 1;
}
...
int MyArray[4] = {1,3,5,7};

ModifyMyArray(MyArray);

Similar to this question in Java - Is Java "pass-by-reference" or "pass-by-value"?


It is true that when you pass an array to a function that you are actually passing a pointer by value. However, I feel that for people just learning C++ they should not have to worry about pointers at all. They learn that you can pass a variable by value or by reference to a function.

By value means that changes to the variable in the function don't affect the original value in the calling function.

By reference means that if the function changes the value of the variable then those changes will be seen in the original calling function.

When an array is a parameter of a function (without the const keyword) then any changes made to the values in the array will be seen in the original calling function. Therefore, we say that arrays are passed by reference by default. This avoids having to explain what pointers are which really isn't that relevant in passing statically declared arrays around, yet it lets people know that if they mess with the values in the array in the called function that those changes will persist in the calling function.

I teach a "first-course" Computer Science C++ course at a top-notch engineering school (our programming team is going to the World Finals in Russia this year). About 90% of the people in the class aren't actually computer related majors (mostly mechanical engineers). The above material is more than enough to confuse them without having to explain what pointers are. That is why that book and others mention that arrays are passed by reference because many people reading the books just need "just enough" C++ to get them by without having to learn every little detail. Those that really want to program should have no problem transitioning to fact that arrays are really passed by pointer.

Just my opinion though.

Tags:

C++

C