Arrays as parameters
As with other variables, you can have array parameters to allow arrays to be passed to functions and procedures. The only real difference is that the array can potentially store significantly more data than other variables. When the array is passed by value, each of its elements must be copied into the parameter. Passing the parameter in this way means that there will be two copies of the data in memory, which takes more time and more memory.
You should avoid passing arrays by value, and instead pass them by reference. When passed by reference, we can think of the array itself being passed across. This gives the called function or procedure access to the data, but does not require that the values be copied across. As we saw before, you can use a const
reference if you do not want the array’s data to be changed.
In C/C++
Ok, this is one of the places where C/C++ does something “interesting”. Because passing an array by value is a bad idea, the language does not allow you to pass arrays by value at all. Instead, when you pass an array to a parameter it will pass the address of the array. This then introduces some inconsistencies in the syntax and the way it treats arrays.
When you pass a value to an array parameter, you do not need to get the address of arrays, as you would do with other types when passing to a pointer. Instead, this is done for you by the compiler. When you pass the array, it gets the address and passes that to the array parameter. Interestingly, this means you can declare the array as either an array or as a pointer. Both work the same, though the array syntax would better communicate your intention.
Example
The following sample code demonstrates passing arrays either using the array or pointer syntax for the parameter. Notice that you can also use the [...]
notation with the pointer, this is a result from the decision to always pass arrays by reference.
Arrays are not pointers
The C/C++ array syntax can result in some confusion about arrays and pointers. Some people say that “arrays are pointers”, but that is not true. The array is a variable, with sufficient storage to store a number of values. When you pass this to a parameter, you get the address being passed across. This does not change the array. Try to keep these clearly in your mind. In C, you can have an array, you just cannot directly pass it to a parameter.
This confusion is understandable. You can pass it to a pointer. So it must be a pointer! You can use []
with pointers. So arrays must be pointers too! No! Sorry, this is just a result of decisions made during the design of this language. By treating arrays differently, the language makes this confusing if you do not have a strong mental model of what is going on.
When you declare an array, picture it as an array. That is the right mental model.
When you pass an array in C, remember it will pass it the pointer to the array. The array is not a pointer, the compiler just has code in it that says “if it is an array, pass the address instead”.