Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays passed by reference by default?

Tags:

c++

c

I'm reading a C++ book which says this:

C++ passes arrays to functions by reference—the called functions can modify the element values in the callers’ original arrays.

It is referring to situations like this:

int hourlyTemperatures[ 24 ];  modifyArray( hourlyTemperatures, 24 ); 

However, this is vanilla C array pointers at work here, right? There is no use of a C++ "reference" technique, what is being passed is a pointer by value, in this case, a pointer to the first element of the array. The end result is that the function does have access to the full original array, like a reference, but this is not actually pass by reference, right?

From this Prentice Hall book: The book in question

like image 727
johnbakers Avatar asked Jan 14 '13 03:01

johnbakers


People also ask

Why are arrays automatically passed by reference?

The only reason for passing an array explicitly by reference is so that you can change the pointer to point to a different array. If a function only looks at the contents of an array, and does not change what is in the array, you usually indicates that by adding const to the parameter.

Are arrays always call by reference?

There is usually no need to pass an array explicitly by reference because arrays are always passed by reference.

Are all arrays pass by reference in C++?

It doesn't happen in C++. There is only pass-by-value and pass-by-reference. Pointers in particular are passed by value.

Is array always passed by reference in Java?

Arrays are Objects, yes, but nothing in Java is passed by reference. All parameter passing is by value. In the case of an Object, what gets passed is a reference to the Object (i.e. a pointer), by value. Passing a reference by value is not the same as pass by reference.


2 Answers

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.

like image 180
bcfehrman Avatar answered Oct 05 '22 05:10

bcfehrman


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.

like image 45
Lightness Races in Orbit Avatar answered Oct 05 '22 05:10

Lightness Races in Orbit