Let's summarize everything we covered in the first module.
Every time we declare a variable,
a place in memory will be allocated to store its contents.
Each place in memory has its own memory address.
We can get that memory address by prepending the reference
operator to the variable's identifier.
Memory addresses can be stored in special type of variables,
which we call pointers.
A pointer is declared just like any other variable with
the addition of the asterisk character.
This pointer will hold a memory address of a place in
memory which can store an integer.
Data type of a pointer has to match the data type of the
variable at which the pointer is pointing to.
After the declaration, once we create a pointer,
we can use the same asterisk character to dereference.
It.
Dereferencing a pointer will allow us to read and write
data to the pointed memory address.
All of the statements placed inside of the curly
braces belong to a separate scope.
This also includes things like loops and function definitions.
Variables declared inside of the specific scope will be removed
when the program reaches the end of that scope.
When we pass arguments to function calls,
we are passing those arguments by value,
which means that we literally copy the value from the arguments place in
memory to the memory allocated by the function's parameter.
From the perspective of function,
we are no longer able to access the initial argument because
function definition operates in a separate scope.
Copying data all around the memory is not always preferred,
especially for large user‑defined data types.
To solve this issue, we can instead pass the argument by reference.
To achieve this,
we can reference the argument and store its memory
address inside of the pointer parameter.
This will allow us to still access the place in memory of the
initial argument by simply dereferencing a pointer.
There is also an easier and better way to do this in
C++. Instead of using pointers,
we can use C++ references. A reference is like an alias for another
variable, and we can create it by prepending the identifier with the &
character inside of the reference declaration.
We need to immediately assign it to another variable, and then we
can use it as a way to access the same place in memory with a
different name. When we make a reference constant, we are informing
the compiler that this reference is not intended to modify the data
that it refers to.
If we put the const keyword before the asterisk
inside of the pointer declaration,
we are saying that this pointer should not be able to
change anything after we dereference it.
Const keyword after the asterisk prevents us from changing the initial
memory address held by the pointer. And that's it.
We are done with the first module.
Passing arguments by reference is not the only purpose of pointers,
which we'll see in the next module. If you have any questions,
don't hesitate to ask them in the discussion.
Thanks for watching, and I will see you in the next module.