Now we have the ability to store the address of a variable
in a separate variable know as pointer variable and through that variable we
can easily access the value of other variable whose address it is storing.(If
you found this statement confusing you probably should give another good read to
this article.)
In this article we would try to break inside the variable
using various ways.
Let’s take a look at the following code:-
- #include<stdio.h>
- void main()
- {
- int var;
- int *p;
- var=5;
- p=&var;
- }
After the execution
of the above code this is how your machine will allocate memory space for both the variable var and p and put 5 inside var and address of var which happens to
be 1000 inside p and as p is a pointer variable it acts as if the variable p is
pointing towards the variable ‘var’.
While in
reality it is just a variable (a space allocated inside the memory of your machine)
that is why the pointer variable p will also have an address.
Now let’s begin with the quest of figuring out
multiple ways to access the address od variable the data contained in the
variable etcetera etcetera.
First and
the easiest way is to directly use the name of the variable :
printf(“%d”,var);
above piece
of code will give the output :5 (the value inside the box named ‘var’).
and to know
the address of the variable ‘var’ we can to use the &-operator(address-of operator) as the name suggests it will help us
to get the address of the variable nothing too tricky here:
printf(“%u”,&var);
above code
will give the output:1000 (the address of ‘var’).
NOTE:-Here I
have use %u as the format specifier to print the address. %u is the
format specifier for unsigned integer. This
is usually used to print addresses because it’s range only has positive values
making the range very large and usually addresses are positive integer number.
Second way ……before
discussing about the second way lets give a quick visit to the world of special
sticky note pads which has the address written down of your friend’s house.(If you need a brush up click here)
Now from
here you can do two things first you can read the address and go near your friends
address and stand outside the door from the sticky note pad or secondly you can
read the address and visit your friends house and get inside the house.
If and only
if you enter the house you would get to know whether your friend is inside the
house of not or whether any another person is inside the house or not.
so if you try
to output the value of ‘p’ by
printf(“%u”,p);
the output
will be:1000 (the contain of the box of the variable p which is equal to the address
of ‘var’)
so printing ‘p’
is as good as reading out the address from sticky note and standing outside of the
memory location of ‘var’ so there is no way you would be able to know the
content of ‘var’.
So now the
moment has come to get inside ‘var’ and get the value which it contains so we
once again use our special symbol ‘*’ to get inside ‘var’.
So the second
way is:
printf(“%d”,*p);
the output
will be :5 (The value at the address pointed or contained in the pointer
variable ‘p’).
Few Graphical illustration will clear out
everything:-
This is you
1.When
printf(“%d”,var);
you are at:
2.when
printf(“%u”,&var);
you are at:
3.when
printf(“%u”,p);
you are at:
4.When
printf(“%d”,*p);
you are at:
Lastly you can
even get to know the address of the pointer variable p.
Do you know
how?
if yes
comment down the answer in the comment section below.
Thank You.
This comment has been removed by the author.
ReplyDelete