The reference operator.
The reference operator is a unary operator that accesses the object to which a pointer refers.
Here, let me take the node.
Here the reference.
Operator here.
So this is the inverse operation of the address of operator, like the address of operator system programmers
use the the reference operator very often.
So many operating system APIs will return pointers.
And if you want to access the referred to object, you will use the the reference operator here.
So unfortunately, the the reference operator can cause a lot of notation based confusion for beginners
because the the reference operator, the pointer declaration and the multiple action all use asterisk.
So remember that um you append an asterisk to the.
End of the pointer to type object's type to declare a pointer.
So however you prepend the dereference operator and asterisk to pointer.
So like this for example my my variable address here.
So.
After accessing an object by Prepending the dereference operator to a pointer, you can treat the result
like any other object of the pointer to type.
So for example, because the my variable is an integer, you can write the value into my variable using
the my variable address pointer here.
So the syntax is like that.
My variable address and here 995 And as you can see here, we wrote this variable by using a pointer,
but we used the dereference operator.
So let's print our variable name, my variable.
Go here.
Let's print our variable value and decimal and.
Here.
My variable.
My variable here.
And run.
And as you can see here, we assigned.
Uh, this variable address using variable address with the reference operator.
So because the reference pointer that is the my variable address appears on the left side of the equal
sign.
You are writing to the address where gets here.
My variable address is stored.
So if the reference pointers appears anywhere except the left side of an equal sign, you are reading
from the address.
So to retrieve the integer pointed to by the my variable address here you can tack on the the reference
operator.
For instance, this code here will print the value stored in the my variable here.
So here.
This small.
Integer or just an integer here.
The.
And my variable address.
Here, and this will take from it.
Here.
Right.
And as you can see here, this cat returned the same number here.
So.
Here I am.
I want to create some code.
Some example code that we will make it happen.
To show how this the reference and address of operators work in depth in C plus plus.
So let's firstly here we will create the.
Integer my variable here and assign it to zero.
Or let's change the name here.
My car.
Assign it to zero.
Or let's change to my age here.
It will be more appropriate for an integer.
So integer.
This the reference pointer here, as you can see here.
Integer.
Uh, my age address.
Here.
So I want to mention that it doesn't matter if you write the in this asterisk operator here or just
after the here, because actually it's just the your you you can write the both sides but it has to
be an integer and the variable name in C plus plus here.
So I will write in this time I will write here my age address address here and my age.
I will explain all of these codes later.
Print f.
Here value at my age address.
Here and we will print that.
A new line here.
We'll use the we will use the Asterix, the reference operator.
And this as you know, this is the address of operator.
We will use both of them in this example code.
My age address and then print f again.
My age.
Address here.
As you know, the format specifier for pointer is P and the new line.
Here.
We will write my address here just then, without the reference of the referencing or without the referencing
or address of operator, because this is my address itself is just a pointer type integer pointer type
here.
So then we will assign, we will use the asterisk here and assign it to nine for example, 1099 here,
ten 1099 and then.
And printf here value.
Value at my my age address here.
Decimal As you know, this is a integer format.
Format specifier for integer and we will use the asterisk.
The reference operator.
My my age address.
My age either.
As you can see, we can't use the my age by itself because in direction requires pointer operand here.
And that's why we will use you.
We will use the integer pointer here and then printf lastly.
Here.
My var address, my age address.
I'm sorry.
My age address here.
Pointer type.
Here.
My age address without any operators.
And let's run our code and I will explain all of this in this lecture.
So.
Uh, first you initialize the my age to zero.
Then you initialize, uh, to the pointer here.
To the pointer.
A my age address to the address of my age.
Here.
So next you print the.
Integer pointed to by address here.
As you can see, we use the asterisk operator here, the reference operator.
And the we will we will print the value of the my age address here.
So then we will write the value 1099 1099 into the memory pointed to my age address and then print the
pointed to value here as you can see here point and the address again.
So then as you can see here, we this is the pointer to type and this is the address here.
So and as you can see here, firstly, my age addresses was equal to zero.
And then.
Uh, here.
The.
Address was same to and after the initialization here or changing the address to.
Changing the variable of my age variable to another var here and value here.
And as you can see, we got the same addresses but different values.