Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
221 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...