Hello and welcome back. In this lecture, I want to provide a demonstration or
solution of our previous challenge which got you familiar with
pointer syntax, the concept of a pointer,
assigning an address to a pointer and then displaying some output for a pointer.
So let's bring up code blocks, and we're going to create a new project a new c project
that will just be called pointer basics, because it's going to do the basics of pointers.
So we know how to do all this. We're going to select console application,
go make sure we have c and just call this
pointer basics.
This pointer concept is really, really important.
So we're starting off slow and going on from there.
So here we go. Here we have our main.
Now all we want to do is we want to create
one integer variable and then one pointer variable.
And we want to initialize both.
So the first thing I'm going to do is I'm going to create an integer variable
and I'm just going to name it number or num.
And I'm going to initialize this to 150.
To create a pointer variable, it has to be the same type.
To create a pointer, all you have to do is do the data type and then the asterisk
in the declaration and then we'll just call this pnum for pointer number.
Usually, you want to start your variables that are pointers with the p prefix.
And it's a good idea to always when you create your pointers initialize them to null.
Null is a special keyword that is
saying that the pointer does not point to anything.
Once we have both of our
variables initialized, our num integer and our num integer pointer,
we then want to assign something to our pointer.
Because right now our pointer is not pointing anything in memory.
And you want to make sure whatever you assign your pointer to that,
that actually has memory location for it,
memory allocated.
So in this case,if we're going to assign it num,
num is an integer on line 6 that has memory allocated for it.
C will automatically allocate memory
for any variable declaration.
Even this pointer on line 7 has memory allocated just to store pointer.
It doesn't necessarily mean it has memory allocated for what it's pointing to.
So to sign pnum,
a value of an address, all we have to do is say pnum
equals ampersand,
which is the address of operator
and we want to assign that the address of num.
Num has a memory already allocated for it and we declared it.
So line 9 is just assigning the address of num to a pointer.
It's as simple as that.
Now when you create a pointer and you assign
an address to it, you could also create your own dynamic memory
and assign that. But that's for another exercise. In this case,
we already have our variable created. We're going to point to that variable's value.
We need to assign the address of num.
Now after we've printed out we've done our assignments, we've initialized everything.
Now we just want to print out some information.
So we're going to use the printf and I'm just going to say
first I want to print out the value of pnum address.
Now what the pnum value is for the address,
but the pnum address. And here we can actually say
the address of
a pointer so we're going to say p. And then all we have to do is put in here,
we have to put in the address of our pointer.
So here we would say address of pnum.
And what is this going to give us, this is going to give us the address of the pointer
that was allocating the memory. It's not the value of
the value of the pointer. The value of the pointer is also going to be an address, it's going to be the address of num.
So here let's first print out num as well then. So
here we're just going to print out num address is.
And again, we want to print out an address say percent p.
And then we're going to say the address of
the address of num.
And the address of num should also match the value of pnum.
The address of pnum will be different because that's the pointer in memory.
So we're going to print out the value of pnum
or I should say the address of pnum.
And then we're also going to print out
the value of the pnum .
variable and this is also going to be an address,
and this is just going to be pnum.
And then the last thing we want to display
is we want to display what the value that pnum is pointing to.
So the value of what pnum is pointing to.
And this is actually going to be the
type what it's pointing to is an integer. So this would be percent d.
And let's actually add some line feeds so we can read this.
And what it's actually pointing to the way you get that value is the indirection operator.
So you do the asterisk and then the pointer name.
And that's going to tell you what the value is pointing to which should be 150.
So just to reiterate here on line 11,
we're printing out the address of num.
That's going to be some number or hexadecimal number.
Then on line 12, we're printing out the address of pnum,
which was just allocated on line seven
because you have to store the pnum pointer somewhere.
Then we print out the value of the pointer which is just the address of num.
And when we print out the address of pnum, we have to use the ampersand.
And then we print out the value of what pnum is pointing to
which is dereferencing using the indirection operator.
So now let's go ahead and build this
and run it and we should see all these values.
So the num address
just this number in memory is 0060FF0C.
The address of pnum is going to be something different,
0060FF08.
The value of pnum which is the address of num should match the first line
and it does FFOC.
And then the value of what pnum is pointing to which is 150.
So a pointer really can have three values. Its address,
the value of the pointer which is another address
and then the value of what that address is pointing to.
And again, ampersand always gives you the address
in memory.
Pnum contains an address. You can print that out just
by saying the name of the pointer.
And then to get the value that it's pointing to, you use the indirection operator,
the asterisk and that will give you that value which is 150.
So these are the basics of pointers, getting you familiar with the syntax.
When to use the address of operator and when to use the indirection operator
and then trying to explain just this concept of a pointer and how a pointer contains a value
of an address, a pointer's value is an address.
So play around with this,
understand this syntax, pointers are really important concept. Thank you.