Source Code
- #include<stdio.h>
- void main()
- {
- int var;
- var=5;
- }
Now we know
that how a variable gets stored inside your machine (if not click here).
So, a memory space is
allocated inside the memory as soon as the variable is variable is declared and
the space is filled with the content when the value is assigned to the variable
in the code and the memory space can be accessed with the name of the variable
or the memory address given to it.
Now, we can
use the variable name and directly find out the value containing inside it with
the following simple code: -
printf(“%d”,var);
but suppose you
want to be adventures and want to go on a adventure of using the memory address
instead of the variable name to access the content of the memory location so
can use write something like
printf(1000);
will this
work?
the answer
is absolutely not because this will only print the number “1000” because your C
Compiler (the thing which translates the code you have written to the code
which your machine will understand) will not be able to understand that by writing
‘1000’ you meant the memory address not the normal numerical value ‘1000’.
More over as
you might remember that this memory address is given at random and uniquely to
each and every variable so you have absolutely no control in choosing the address
number and still now you don’t have any way to
even know what is the memory address of the variable you have declared.
So, to complete
your adventure you have to overcome all those hurdles.
First things
first somehow, we need to find out the exact value of the address location so
that we can use it but how?
Let’s get
back to the our world of unique house names (refer to here),suppose you want to
go to your friends house but you don’t have the street number and pin code the
only thing you have is the house name so there is not much of a choice for you.
You have to go to your friend’s house(because in this world telephones are not
yet invented) using the name of the house only but here’s where you use your
smartness. Once you reach your friends house you quickly ask your friend about the
pin code and street number and note down in a special piece of paper (which might
be sticky note pad which you keep inside your pocket), and why is this special?
because you always write all of your friend’s street number and pin code in
this sticky note pad.
In this
world everybody does the everybody does the same. For the first time they visit
their friend or relatives or anyone’s house using the house name and once they
reach there they quickly save the street number and pin code in one of the pages
of the sticky note pad. So sticky note pads are pretty special in this world
because the numbers written on each page might seem random but everybody I this
world knows that the number is definitely some one’s address.
Finally, our
world has given us the way to overcome our problems which we were facing in our
adventure.
So, it is well
established that you cannot directly get the address of the variable without visiting
the memory location at least once with the help of variable name. And also, we
need to have a special sticky note pad in which we can store the memory address
and it needs to be special so that C compiler understand that whatever is written
or stored in the pad is a memory address and nothing else.
In C this ‘special
note pad’ is known as POINTER. It is basically a special variable in which a
memory address of another variable is stored. Let the name of the pointer be ‘p’
which stores the address of the variable ‘var’ which was ‘1000’.
So now the
problem arises how to distinguish a normal variable which will contain a value from
a special variable (pointer) which will hold the address of a normal variable.
That we do
with by using the symbol ‘*’ before the pointer name. So we will declare a pointer
p as ‘*p’.
And then we
will visit the variable with the variable name and store the address with the ‘&’(address
of)operator.
Source code:
- #include<stdio.h>
- void main()
- {
- int var;
- int *p;
- var=5;
- p=&var;
- }
In fourth
line we have declared a normal variable ‘var’
then in fifth
line we have declared a pointer variable ‘p’
in the sixth
line we have initialized the value of ‘var’ with five
and at last
in the seventh line first we visited the variable var by writing ‘var’ , then
we took the address of the variable by writing the address-of operator(&)
before the ‘var’ then finally we saved the address we got from var to the
pointer variable ‘p’. Let’s see how this setup look inside our machine.(The arrow indiates that now ‘p’ contains the address
of var and in a way ‘p’ is pointing towards the variable ‘var’)
A Little
About Pointer ‘P’:
p itself is
a kind of variable in C and it will also contains a value so your machine will
alson allocate a memory space for the variable ‘p’ who itself will have a
unique address.
And if you can explain what is &-operator then please go ahead and explain it in the comment section below.
Thank You.
No comments:
Post a Comment