We are almost done with the first module. I just want to mention how
the keyword const works in relation to pointers and references. When we
use the const keyword in our regular variable declaration, we are
making a promise that this initial value will not be changed throughout
the rest of the program.
If you try to change it,
compiler will remind you that this variable was not intended to be modified.
Since references are just aliases for some other variable, the const keyword
will basically affect them in the same way.
If I put const in the reference declaration,
I'm making a promise that I will not modify the value
which this reference is referring to.
However, since this variable also has access to the same place in memory,
we could use it to change this value anyway. This is why we say
that the const keyword is just a promise.
There are still ways like this one to change the value at the place
in memory which was meant to be constant, however,
this is really not the common use case. Instead,
let's say that we created a function which will print out the
contents of our variable from the parameter.
Since references are mostly used in this context,
it makes more sense to combine them with the const keyword in this situation.
Now you can pass arguments by reference, but you can be sure that the
function will not be able to modify the contents of that argument, it will
only be able to read the data from that place in memory.
In other words, it is like you created a temporary second name for
that argument which is constant only inside of this function and
this makes sense because this function should not be making any
changes to that data anyway.
We'll talk more about constant references in the following modules.
Const with pointers is a little bit more complicated
because pointers are a little bit more complicated than
references and regular variables.
When you declare constant pointers,
you should only pay attention to one thing is the const keyword
placed before or after the asterisk character.
If the const keyword is placed before the asterisk,
that means that it is closer to the data type.
It does not matter if you put it before or after it. As
long as it is before the asterisk, it offers the same functionality.
It means that you cannot change the data at which the pointer is pointing to.
In other words,
if you try to dereference this pointer and assign it another value,
compiler will remind you that you made a promise and that this is not allowed.
However,
it is still completely legal to dereference this pointer for reading purposes,
you are just not allowed to modify that data.
If we put a const keyword after the asterisk closer to the pointer identifier,
that means that we don't want it to point to anything else.
In other words, the memory address that this pointer holds should not be changed.
However,
we can still dereference the pointer and modify the value from that address.
We are just not allowed to point to some other place in memory.
Of course, you can combine both of these approaches so that your
pointer is secure from any modifications whatsoever.
The use case for this will again be the passing of an argument by reference.
If we put const before the asterisk,
we are basically doing the same thing we did with references.
We are making sure that this function cannot change the data
that this pointer is pointing to.
In most situations,
it also makes sense to put the const after the asterisk so
that we can be sure that we never lose the address held by
the pointer. And that's it.
We finally reached the end of this first module.
It is essential that you understand these concepts so that we
can build upon them in the following modules.
If you have any questions whatsoever,
don't hesitate to ask them in the courses discussion
and please don't skip the summary.
It can help you solidify the knowledge before moving on with the course.