Lecture 15
Passing
Array Elements to a Function
Array elements can be passed to a function by
calling the function by value, or by reference. In the call by value we pass
values of array elements to the function, whereas in the call by reference we
pass addresses of array elements to the function. These two calls are
illustrated below:
And here’s
the output...
Here, we are passing an individual array
element at a time to the function display( ) and getting it printed in
the function display( ). Note that since at a time only one element is
being passed, this element is collected in an ordinary integer variable m,
in the function display( ).
And now the
call by reference.
And here’s
the output...
Here, we are passing addresses of individual
array elements to the function display( ). Hence, the variable in which
this address is collected (n) is declared as a pointer variable. And
since n contains the address of array element, to print out the array
element we are using the ‘value at address’ operator (*).
Po inters and Arrays
To be able
to see what pointers have got to do with arrays, let us first learn some
pointer arithmetic. Consider the following example:
Here is the
output of the program.
Observe the last three lines of the output.
65526 is original value in x plus 2, 65524 is original value in y plus
4, and 65520 is original value in z plus 1. This so happens because
every time a pointer is incremented it points to the immediately next location
of its type. That is why, when the integer pointer x is incremented, it
points to an address two locations after the current location, since an int is
always 2 bytes long (under Windows/Linux since int is 4 bytes long, new
value of x would be 65528). Similarly, y points to an address 4
locations after the current location and z points 1 location after the
current location. This is a very important result and can be effectively used
while passing the entire array to a function.
The way a pointer can be incremented, it can be
decremented as well, to point to earlier locations. Thus, the following
operations can be performed on a pointer:
(a) Addition of a number to a pointer. For
example,
(b)
Subtraction of a number from a pointer. For example,
Now we will
try to correlate the following two facts, which we have learnt above:
(a) Array
elements are always stored in contiguous memory locations.
(b) A
pointer when incremented always points to an immediately next location of its
type.
Suppose we
have an array num[ ] = { 24, 34, 12, 44, 56, 17 }. The following figure
shows how this array is located in memory.
Here is a program that prints out the memory
locations in which the elements of this array are stored.
Note that the
array elements are stored in contiguous memory locations, each element
occupying two bytes, since it is an integer array. When you run this program, you may
get different addresses, but what is certain is that each subsequent address
would be 2 bytes (4 bytes under Windows/Linux) greater than its immediate
predecessor.
Our next two programs show ways in which
we can access the elements of this array.
This method of accessing array elements by
using subscripted variables is already known to us. This method has in fact
been given here for easy comparison with the next method, which accesses the
array elements using pointers.
Obviously, a question arises as to which of the
above two methods should be used when? Accessing array elements by pointers is always
faster than accessing them by subscripts. However, from the point of view
of convenience in programming we should observe the following:
Array elements should be accessed using
pointers if the elements are to be accessed in a fixed order, say from
beginning to end, or from end to beginning, or every alternate element or any
such definite logic.
Instead, it would be easier to access the
elements using a subscript if there is no fixed logic in accessing the
elements. However, in this case also, accessing the elements by pointers would
work faster than subscripts.
Passing
Entire Array to Function
we saw two programs—one in which we passed
individual elements of an array to a function, and another in which we passed
addresses of individual elements to a function. Let us now see how to pass an
entire array to a function rather than its individual elements. Consider the
following example:
Here, the display(
) function is used to print out the array elements. Note that the address
of the zeroth element is being passed to the display( ) function. The for
loop is same as the one used in the earlier program to access the array
elements using pointers. Thus, just passing the address of the zeroth element
of the array to a function is as good as passing the entire array to the
function. It is also necessary to pass the total number of elements in the
array, otherwise the display( ) function would not know when to
terminate the for loop. Note that the address of the zeroth element
(many a times called the base address) can also be passed by just passing the
name of the array. Thus, the following two function calls are same:
Array of Po inters
The way there can be an array of ints or
an array of floats, similarly there can be an array of pointers. Since a
pointer variable always contains an address, an array of pointers would be
nothing but a collection of addresses. The addresses present in the array of
pointers can be addresses of isolated variables or addresses of array elements
or any other addresses. All rules that apply to an ordinary array apply to the
array of pointers as well. I think a program would clarify the concept.
As you can observe, arr contains
addresses of isolated int variables i, j, k and l.
The for loop in the program picks up the addresses present in arr and
prints the values present at these addresses.
An array of pointers can even contain the
addresses of other arrays. The following program would justify this.
Try to guess what the output will be.
2 comments:
Very informative keep blogging.aircon chemical wash singapore
Very informative keep blogging.chemical overhaul
Post a Comment