- Now we started get to the fun part about Pointer.
Pointer arithmetic.
Let's see what this is about.
One of the most important features of pointers
is that they are closely related to arrays.
If you recall, an array, it's essentially
a collection of elements grouped together,
as long as they have the same data type.
An array is actually a closed depiction
of the memory of a computer.
When you are storing numbers into an integer
array for example, "sizes", essentially you are
grouping a set of memory locations under the label, "sizes,"
and then you refer to them as,
sizes zero, sizes one, and sizes two,
to correspond to the three elements
stored in that array.
We can now use a pointer to point to the beginning
of that array.
This is a pointer of type "int" because the array is
also of type "int", and the pointer needs to be
able to point to addresses of the same type
as they array.
Notice that the variable "sizes" without the brackets
is the storage place for the first address
at which the array starts.
Now we have a hook, we have a "ptr" variable,
a pointer, the one with the star in front of it,
that tells us exactly where the array begins.
If we are incrementing the "ptr" or pointer variable,
we are not adding anything to the array.
What we are doing instead, we are telling the pointer
to advance to the next value of the array.
Whether that value is a contiguous part of memory
or jumps to a totally different location,
it's inconsequential at this point.
We are treating the group of three numbers
as three values stored in memory,
and we can move from one to another
using in a sense, the pointer as an index to the array.
We can then move back to the first value of the array,
because we are not using the "--",
and this is now a mechanism that lets us point
to specific items in the array,
referring to them using the pointer.
Let's look into the code and see what actually
is being stored at the pointer address.
I'm declaring, again, an array "sizes",
and storing inside it three numbers, 15, 20, and 30.
On line 17 we are declaring a pointer,
and are initializing it with the beginning of the array.
By assigning the value "sizes" without the brackets,
we are telling the pointer that
this is where the array starts.
We now have a hook at the start of the array.
On line 20 we are incrementing the pointer.
On line 23 we are incrementing it again,
and I will show you that incrementing the pointer
allows us to point at various elements of the array.
Let's run the program and look at the code in parallel.
The first "printf" statement that's the address
of the beginning of the array,
and indeed, we see that at Address 58ca5c78
stores the value 15 which is also
the first element of the array.
We are now moving to the next Address
which is the value 20, and we move there in line 20
where we incremented the pointer by one.
Incrementing the pointer is equivalent to moving
to the next element of the array.
I am repeating that procedure again in line 23
so that we can move to the third value of the array,
and if you notice, 15, 20, and 30,
the values that are outputted by this program,
are exactly the numbers stored in the array.
Finally, I want to go back to the beginning of the array.
Since in line 20, and in line 23
I incremented the pointer twice,
I have to decrement the pointer twice as well.
Line 26 , and line 27 decrements the pointer twice,
and when you print it, in line 28 you can see that
it again points at the value 15,
which is stored at the beginning of the array.