Hello, and welcome back.
In this lecture, I want to talk about accessing the value
that a pointer is pointing to
So remember pointers, they store memory addresses.
That memory address actually contains a value of somewhere.
And so pointers point to that memory address. I want to get the value
of what it's pointing to.
Because I would just print out the value of a pointer that would be an address, but I want to get the address,
I want to get the value of the address.
So you can access the value of an address that a pointer is pointing to
using the indirection operator. The indirection operator is the asterisk.
So we learned how to use the asterisk to declare a pointer,
we're also going to use the asterisk to find the value that a pointer is pointing to.
So it's also referred to as the dereference operator
because you use it to dereference a pointer.
So for example, if you have a number it's equal to 15, you declare
pointer of type int, pointing to an int and you assign it the address of number using the ampersand.
Now how do you get the value of what pointer is pointing to.
You can de-reference.
It you can basically what the value that the pointer is pointing to
is the value is 15 because it's pointing to the address of number which is 15.
So the pointer variable contains the address of the variable number.
You can use this in an expression to calculate a new value for the result.
So say we wanted to do result equals
the value that the pointer is pointing to plus five. You would use the asterisk for
it before the name of the pointer.
And so what this would do is it would basically say what is pointer
variable pointing to, it's pointing to 15 add five to it.
It's the address
that the pointer is pointing to what is the value of that address.
So the indirection operator
asterisk you should notice is also the symbol that's used for multiplication,
and it's also used when you declare pointer types. So this can get very confusing.
How do you know necessarily what's going on or how does the compiler work.
Well, it's all about context. It depends on when you use that asterisk and where you use it.
If you use the asterisk when you declare a pointer right after the type
of the pointer,
then it's obviously using a declaration.
If you use the asterisk in between two numbers,
then it's being used for multiplication. And if it's used
right before the pointer name on the right hand side,for example, or the left hand side,
it's going to be assigning a value or accessing a value.
So it's all about context when you use these this
indirection operator.
And that's how it works.
So that's how the compiler is going to interpret it.
Whenever you see it in, this example, star pointer
and you don't have a data type before or you don't have two numbers on each side,
then you know it's an indirection pointer, dereferencing.
So let's go through an example. Here's an another example
where we're going to create an integer count
equal to 10. We're also going to create an integer of type named x another variable.
We're initializing count to 10, x is initialized to nothing.
We then create an int pointer. We know it's an int pointer because
we have the asterisk after the data type
and then we named it int pointer.
We're now going to assign to that int pointer the address of count.
Because remember, pointers only contain addresses as their values,
but they also are pointing to something.
So we can then assign the variable x whatever int pointer is pointing to,
what is the value of the address that is contained in pointer.
So x is going to actually display 10.
10. So if we go ahead and display that saying count percent d
ird x percent ird, count is equal to 10, x is going to equal to 10.
That's how you can dereference a pointer and get the value that. It's pointing to
if you want to display the pointer's values because remember a pointer
has a value in memory,
it's just an address. So what if you want to display
the pointers value, which represented by
a hexadecimal number integer and address?
Well, you can use the format specifier percent p. We talked about this before.
This will output a pointer value as a memory address in hexadecimal form.
So if we were to do something like this
where, we declare our number,
we declare our pointer we initialize it to null because that's good practice we want to always do that
whenever you're not assigning something in a declaration to the pointer, make sure you
initialize it to null.
We then assign our number variable 10.
And then we assign our pointer to the address of number.
So the value of p number is actually going to be an address.
an address. So if we were to try to print it out, we would use percent p and that would be the value of the address.
If we wanted to print out what it's pointing to,
we'd have to de-reference it using the indirection operator.
But when you print out the address, what it does is it prints it out in hexadecimal
and pointers occupy 8 bytes in memory,
and addresses have 16
hexadecimal digits. So how does that work?
Well, it depends on whether or not your machine supports 64-bit or 32-bit addresses.
If your machine has 64-bit operating system,
then you're going to see 16 hexadecimal digits.
If your machine or your OS only has 32-bit addressing,
then you'll see the hexadecimal represented as 8 hexadecimal digits.
Now what if you want to display an address.
You may say what's the difference because the pointer has a value.
But you can also display the address with the address of operator.
So in this example, we want to display the address of number.
Number is not a pointer. Number is just an integer.
If we want to get the address of that, we use the ampersand.
If we want to display the address of that, you just use percent p again.
You just apply the ampersand.
So if we supply the ampersand with the pointer variable, we would actually
display the address of that pointer,
not the value of the point. Remember the address of the pointer is some address in memory.
The pointer value is an address that it's pointing to.
And the value that it's pointing to is somewhere else in memory.
So remember that illustration where we showed
how pointers work.
A pointer, when you declare it, creates memory for that pointer,
8 bytes.
The value of that pointer is an address.
And then whatever that pointer is pointing to
is a value, usually of the type that you provided.
But remember that, that there's really three parts of it.
If you want to print out the p numbers address, you again would use the ampersand.
That would be the address of the actual pointer.
And you would have to use the void star to get around a warning.
So remember, a pointer itself has an address, just like any other variable,
it gets allocated when you declare it.
You use percent p as a conversion specifier to display addresses.
You use the ampersand the address of operator to reference the address
that the p number variable occupies.
That's the address of the pointer. Then again, the pointer
has a value of an address.
The cast a void is to prevent a warning from the compiler,
specifically the percent p specification expects the value to be some kind of pointer type.
But the type of ampersand number is a pointer to a pointer of it.
And so you have to cast it.
Displaying the number of bytes a pointer is using.
Another way to get information about a pointer.
Here you can use the sizeof operator. We've learned about the sizeof operator
to display different data types bytes. You can do the same thing with a pointer.
On a machine that I'm running, this will occupy 8 bytes.
And the machine address has 64 bits because it's a 64-bit
operating system.
And you may get a compiler warning when using sizeof this way,
sizet is an implementation defined integer type.
To prevent the warning, you could cast the argument to a type int like this.
So what you would do is you would say p number size, percent d bytes.
And you would say int, cast it to an inch sizeof p number.
That's going to give you the size of the pointer in memory,
size of pointer of that address, which, again, is eight bytes on most systems.
Because that's just containing an address.
Another example here. This is going to display a number of things.
And we'll run this example to show the output.
But just before we run it, I want to describe a little what's going on here. We have a number that's declared.
We have a pointer that's initialized the null.
We set the value of number to 10.
And then what we want to do is we want to print out the address
of the integer number, and we want to print out the value of number,
very simple, using the address of operator and then just the variable.
When we do the address of operator, we do percent p.
We're then going to assign the address of number to our pointer.
We're then going to print out some pointer information.
We want to output the address of the pointer itself.
We can just use percent p, void star ampersand number.
We want to see how many bytes that pointer has in memory. We can say sizeof.
We want to see the actual value of the pointer,
which is an address. We can just say p number without any
extra information and then we use percent p.
If we want to see the value that the pointer is pointing to,
the value at the address, then we do
star p number, we use the indirection operator.
And we know that p number is pointing to an in so we can say percent d.
So let's take a look at this example in code blocks to see how it's actually displayed.
What we're going to see is some hexadecimal numbers
for addresses whenever we display addresses and we're going to see values in other cases.
So this should look familiar. This is just a program
that is showing you how to access different parts of a pointer.
So again, we have a regular number data type int, and
we have our pointer. We initialize it to null.
We're first going to display our number, which is 10. So this is going to display the address of number,
and then it's going to display the value of number on lines 10 and 11.
We're then going to assign our pointer, the address of number.
If we want to see the actual address of our pointer, we just use the ampersand,
the address of operator, and we cast it to void star.
We use percent p.
If we want to see how many bytes the pointer is in memory we do percent d sizeof.
If we want to see the address the value of peanut the pointer,
we do percent p and we just say p number, because p number is an address.
If we want to see what the pointer is pointing to with the value of the address
that's in the value of pointer, we just say asterisk p number,
we dereference it, the indirection operator.
So let's run this.
When I run this, you'll see the first two things that are printed out,
the numbers address which is on line six,
and that's some hexadecimal number, 0060ff0c.
And this is actually running a 32-bit program, so you see it's only
8 bits.
the number variable which is 10.
We then print out the address of the actual pointer to allocated memory.
It's a different address than number.
Because this is the address allocated for the pointer.
The pointer number size is 4 bytes,
and that means it's a 32-bit addressing.
The pointer's value, which is the address
of number, it should be the same, and it is, 0060ff0c.
And you see that you can actually get the
number address by just using the name, the pointer with percent p.
We saw size of operator 4 bytes.
If you want to get the value that p number is pointing to which is 10,
you have to de-reference it using the indirection operator and you see that it's printed 10.
But you see you use percent p whenever you're displaying addresses,
use the percent e when you're displaying values of the integer pointer
or the number of bytes.
So this is the basic syntax of accessing pointer values.
Address of pointer, ampersand,
value that the point is pointing to, just the name of the variable
and the value of the pointer itself is just the name of the variable,
then the value that the pointer is pointing to use the indirection operator.
Thank you. Hope this helps. Study this, run this through.