- We can now better understand the relationship
between pointer and arrays
by looking at ways in which we can move across the array
and point at the addresses that the array stores.
Let's look at a few characteristics of that relationship.
For example, the arrays store data in a sequential block
which is very helpful if you are going to refer to them
starting with one particular location
and then advance one position at a time.
Pointers are variables that point to the beginning
address of an array and retrieve the values stored
at that address.
An array notation is essentially a pointer notation.
Let's see how pointers enable powerful access
to memory and the easiest way to do that is in code.
I defined an array named data, it's an array of int
that has six values and a pointer
who has been assigned that location of data
so now the variable pointer, ptr with a star,
points to the beginning of the array.
Now I'm going to use an interesting property of the array,
I would like to know what is the length of this array.
I'm going to do this in the following way in line 21.
I am dividing the size of the entire array
using size of data,
it's a function we encounter in one of the earlier videos
when we discussed the sizes of various data types.
I am dividing the size of the entire array
by the size of int which is the data type of the array.
Because each one of the elements of the array
has exactly the same data type
and the factor that distinguishes one data type from another
is its size.
If I have the sum of sizes, I'll divide that
by one of the sizes,
the result is the number of elements in that array.
We will verify that when we look at the code
that prints in line 22, the number of elements.
Finally, now that I know how many elements are in the array,
I can use it in line 24 to write a for loop
that will traverse the array
and then use the pointer in line 26
to point to each element of the array
at every cycle of the for loop
and print the content that's being stored at that location.
This way, I am able to point to the specific elements
in the array without using the array name,
without using an index to the array,
and essentially I am showing that array notation
it's pointer notation.
I can either refer to that notation as data with an index
or I can refer it to a pointer
and whatever the pointer points to.
Essentially we have two labeling systems
for the same exact data structure.
Let's run the program and see what happens.
If you look at line 18,
print data start at address and so on.
If you look at the first line of the output,
it tells us that data starts at address
so and so 5761160
with the value 15
which is the beginning of the array.
There are a total of six elements in the array,
we computed that in line 21 by dividing
the entire size of the array by the size of one element
and it has indeed been confirmed
that the array has six elements.
I am now able to use that number six in line 24
where I am performing a for loop
starting with location zero
all the way to numElements minus one
because i is less the numElements
that's the boundary to finish the loop.
I am incrementing the pointer,
first I can print the value of the pointer
which is the address and what it points to
that's the star ptr
because I already have a pointer
to the beginning of the array.
Then in line 26, I increment the pointer
and continue the loop and keep printing
all the elements of the array
and their respective addresses.