By: Asaye Chemeda Email:
[email protected] 31
The above program calculates the cube roots of the first
five positive perfect cubes by the cubeRoot function
and prints the cube roots by the print function. We will
only discuss the important points in the program.
On line 11, the cubeRoot function is called and the
identifier of the cube array is passed as an argument.
Technically, what is passed as an argument is a pointer to
the first element of the cube array. When the
cubeRoot function is invoked by this call, the first thing
the function is going to do is assigning its formal
parameter accordingly with the passed argument. In C++
statement, this assignment is represented as:
Isn’t this how a pointer is declared initialized with another
pointer? Of course it is. In the above declaration, we need
to understand that the data type of c is double and it can
only be initialized with a pointer of only double data type.
After the above declaration the pointer c will contain the
memory address of the first element of the cube array.
On line 17, the cube root of the value at address contained
by c is calculated and stored in the same address
contained by c. On line 18, the pointer is incremented.
After each increment, the pointer c will point to the next
element in the contiguous memory location, i.e., the next
element of cube array. The for-loop controls for how
many times that the increment will be done. When the
loop is complete, all the elements of the cube array will
now contain the cube roots of the corresponding original
values. This change is also reflected in the main function
where the cubeRoot function is called. Therefore,
when the array identifier cube is now passed to the print
function on line 12, it refers to the memory address of the
first element of the changed cube array elements not those
elements given on line 10.
Note that the data type of the formal parameter of the
print function is void. This means that, the cubeRt
pointer is a void pointer which can be assigned with
another pointer which points to a variable of any data
type. However, to access and manipulate the data
contained by the cubeRt pointer, explicit type casting
should be made. Type casting is conversion of one data
type into another. One way to do this in C++ is by putting
the data type to be converted to within brackets in front
of the variable which we want to convert its data type as
it was done on line 22. However, type casting may not be
applicable for conversion between some data types. Note
that the casting on line 22 could also be done by using the
static_cast key word and the data type to be casted
to within angled brackets as in the following statement:
On line 22 of the program, the void cubeRt pointer is
casted or converted to double pointer and the casted
pointer is assigned to a double pointer named x. Through
pointer x, the elements of the array x can now be
accessed and printed by using the increment operator.
The part of the statement on line 24, *x++, means access
the value stored at address contained by x and increment
the pointer x to the next value.
I hope the above example clears any doubts you might
have regarding pointer incrementing and passing one-
dimensional arrays to functions as pointers. Remember,
we haven’t yet covered all the possible arithmetic
operations on pointers; two more are remaining.
Adding or subtracting integers from pointer
variables. If you understand how incrementing and
decrementing of pointers is done, understanding how
adding or subtracting of integers is done is pretty easy. As
you know, when incrementing is done by the ++ operator,
the value of the variable on which the incrementing is
done will be increased by sizeof(base data type of the
variable). The keyword sizeof is a reserved C++ word
used to return the size of the memory space allocated for
objects. The value sizeof(base data type) of may not
merely mean one. It may mean one for char pointers,
four for int pointers or eight for double pointers.
Adding an integer i to a pointer, therefore, means adding
i times size of the pointer variable. The same is true for
subtracting integers. Let us see this through the following
piece of code.
In the above piece of code, a double variable x is defined
and initialized on line 1. On line 2, a double pointer p is
defined and initialized with the address of x. Say the
address of x is 100. After line 2, the value of p will be 100.
Double data types normally require 8bytes. A single
increment on pointer p by ++ will result in a value of p
to be 108. However, on line 3, the value of p is increased
by 10. What do you think will the value of p become after
line 3 when the value of p after line 2 is 100? Are you
saying 110? If you say so, you might have forgotten how
incrementing of pointers is done.
When 10 is added to a double data type pointer variable,
the value of the pointer will be incremented by 10 times
sizeof(double) (i.e., 8). In other words, when 10 is
added to pointer p, the value of the pointer will be
incremented by 80. Therefore after line 3, the value of p
becomes 180.
Subtracting integers from pointers is also done in a similar
procedure.