Hello, and welcome back.
In this lecture, I want to talk about the const modifier
and how we use it with pointers.
We've learned about constant previous lectures.
It just means you can't change the value of a variable. But what does it mean when we apply it to a pointer?
So when we use the const modifier in a variable or an array,
it tells the compiler that the contents of the variable array will not be changed by the program.
The alternative was to create define statements
by the preprocessor, but const is usually more efficient better way to do it.
With pointers, we have to consider two things when using a const modifier.
Are we actually saying that the pointer cannot be changed?
Or are we saying the value that the pointer points to cannot be changed?
Because remember a pointer contains a value of an address.
And we may not want to change that address ever.
Or we may not want to change the value that the point is pointing to.
In either case, we have to be specific in our syntax.
So you can use the const keyword when you declare a pointer to indicate that the value pointed to
must not be changed.
The way that you provide this and again so the pointer
is a value as a value has an address.
What it's pointing to is the value of that address.
If you want to have that not change,
you would do something like this.
You would say when you declare the pointer you would put the asterisk
in the constant modifier before the type.
So you would say constant whatever the type your pointer is asterisk pointer.
So this is actually deformed defining a pointer
to a constant.
We're not going to be able to change 999l.
So we declare that the value pointed to by the pvalue to be constant.
The compiler will check for any statements that attempt to modify the value pointed to by the pvalue.
And if there's any buddy that does in the program, it's going to throw an error.
So this is the syntax. Again what you want to notice here
is the const is the first word,
and it's before the type. So that's saying that the value pointing to
cannot change.
So if you try to do something like this on pvalue,
you try assigning a value to what's pointing to because you
see the indirection operator there.That means we're
assigning a value to what we're pointing to. If we try to do that,
it's going to give an error because we can't change that value,
can't change 999l to 888l.
So now what about pointers to constants.
And in the previous declaration,
we created the pointer that the point is pointing to cannot change.
But we can still change the actual value of the variable that we declared.
So we would still be able to change long value.
We can't change star p-value.
But long value is not defined as a constant it's just defined as long value.
So we can still modify the value integer pointer
because it didn't have constant applied.
We only applied constant to the pointer. So we can say value equals 777l.
The value pointed to has changed, but you did not use the pointer to make the change.
The pointer itself is also not a constant.
The pointer remember contains an address.
We didn't declare the pointer to be a constant.
We what the pointer is pointing to to be constant. . So we can still change that as well
so. If we declared a new variable called number,
we can say p-value equals the address of that number.
That's perfectly fine because we're changing the address of the value of the pointer.
We're not changing the value of what the pointer was pointing to.
So understand this distinction,
either changing the value of the pointer, which is just an address
or changing what the pointer is pointing to.
If you don't want to change what the pointer is pointing to,
const has to be that first keyword in the declaration of the pointer itself.
So again, in this example, we're going to change the address stored in p-value to point to a number.
We still cannot use the pointer to change the value that is stored
because that was declared as we can't change that value.
You can change the address stored in the pointer as much as you like, just like we did.
Using the pointer to change the value pointed to is not allowed even after you have changed
the address stored in the pointer.
So even though we just changed it to long number,
the pointer still had constant as the keyword.
So we can't change the value no matter what the pointer is pointing to.
What about not allowing the address of the pointer to change,
the value that's in the pointer.
So you also may want to ensure that the address stored in the pointer cannot be changed.
You can do this by using the constant keyword in the declaration of the pointer
the same way, but it's slightly different.
Instead of having const as the first keyword,
you put it after the data type and after the asterisk.
This is saying that the pointer itself cannot change its value.
If you put it before the int, you're saying the value pointed to by the pointer cannot change.
So when you do this, it ensures that the pointer always points to the same thing.
The address will never change.
The compiler will check that you not inverting attempt to change the pointer
elsewhere in your code.
You can't change this address.
So if you tried to say p count equals and item, you would get an error.
Because that can only ever point to the count address.
It's all about where you place the constant keyword if you want to change.
If you do not want to allow the pointer to the pointer to change
or if you do not allow the address of the pointer to change.
Constant as the first keyword doesn't allow the value to the pointers pointing to to be changed.
If you put the constant after the data type in the star,
that's saying the pointer address can't change.
So that's a lot to take and that's very confusing, so study what I just said.
Understand that syntax, it's very important
and it's very tricky. It can be very
easily make that mistake. So understand the difference.
You can still change the value that p count points to using p count,
just like we could in the previous one. Because p count is,
even though we assign we can't assign a new address,
we can still change the value that's pointing to.
So we can de-reference it and change it that way. We can say star p count equals 345, that's fine.
References the value stored in count through the pointer and changes the value to 345.
You can create a constant pointer that points to a value that is also a constant,
so you could do something
like this, constant in star, constant p item.
And that's saying what the value is pointing to is constant
and the pointer itself is a constant.
So p item is a constant pointer to a constant so everything is fixed.
You cannot change the address stored in p item and you cannot
use p item to modify what it points to.
However, you could still change the item.
Item is not a constant, so you could change item.
You can change the value directly.
If you wanted to make sure nothing was changed,
you'd have to actually create the item int variable as a constant as well.
And then that would say it's never going to ever change.
But remember the difference here between constant pointers and constant
values that the pointer is pointing to.
You can use both. You can use one or the other.
If you wanted to ensure that everything's constant, make sure what you're pointing to
that variable that variable is constant as well. Thank you.