- Let's continue our discussion of pointers.
In this video, we will look at
how to use a pointer variable.
Again, we are going to compare variables
versus pointer variables.
When we declare them, we declare a size equals 15
as an integer variable.
We are now going to declare a pointer also to an int.
So far, these two are unrelated.
One is a variable.
One is a pointer that is capable of pointing
to an address of type int.
Now we're actually going to assign a particular address,
that of that previously declared variable size.
Now, pointer size is a pair.
Pointer stores the address of the variable size,
at which 15 is being stored.
To confirm that, we're going to use a simple printf command
to print the value at the address the pointer points to.
Let's see how this works in code.
We have the same exact declaration.
We declare int size equals 15 in line 16,
then we have a pointer of type int,
and finally, we assign the address of that number 15
to the pointer, ptr.
Now we're going to instruct the compiler
to fetch from memory the label of the address
where the number 15 is being stored.
We refer to that address in our program
as the variable size, however, there is a hexadecimal number
that's internally represented by the modules
of the operating systems that manage memory.
Let's run this code first,
and then we'll take a look at line 21
and explain in details what happens there.
As you can see, the address, 54de1c88, contains 15.
The number, 54de1c88, it's a hexadecimal number
that represents some memory location.
We do not control necessarily where numbers are stored,
but we can definitely know where they are.
Let's look at line 21.
Notice the directive %x.
Whenever we want to refer to a pointer,
we are using x and int, in that case.
This particular pointer is a pointer to int,
which means itself is just like an int variable,
therefore, we are using the directive d for the *ptr.
If you look at line 18, ptr without the star
is a variable that stores the address of another int,
therefore, when we write in the print of statement %x,
we use it in conjunction with a pointer,
and this extracts from the ptr variable its content,
which in this case is the address.
To recap, *ptr is the pointer.
Ptr is the variable associated with the pointer definition
that stores the actual memory address,
and as you can see, it's a hexadecimal number,
and at that address in memory, we have the number 15.
Without pointers, we would refer to that location
as the integer variable size.
We have no idea where in memory it is located,
and until now, we did not need to know.
Now that we have the power to manage
memory directly and read,
and we'll see soon, place values at specific location,
this allows for all kinds of interesting things
that we can do in C.
They are as interesting as they are dangerous,
so I'm going to caution you to be very careful
when you use pointers, because the results
are not always predictable for the novice programmer,
and as you have seen before, the computer and the compiler
assumes you know what you're doing
and will not always issue an error message
if something goes wrong.
Something may still be right from a syntax standpoint,
but be wrong from a logical of the program standpoint.