You may not be a C or C++ programmer and thinking
there’s no need for this but knowledge of pointers is useful for any programmer
irrespective of the language they use. Usually people tend to avoid pointers
when they are learning c language because pointers are abstract thought of
memory layout but it is a foundation of good programming principals. So, let’s
understand what pointers are and how it works.
And before we get into knowing pointers, we have to
know how memory works. See when we run a program we use memory to store
variables and such. The memory a program uses is divided into blocks and these
blocks are of different sizes of 32biots or 64bits depending on your operating
system. All the blocks has a specific address associated with it and that’s why
we can access every location of these blocks using its address. And as these
addresses are in hexadecimal so, it is difficult to deal with them directly and
that’s why we access memory in program through variables. Variables
symbolically represents address in the memory and when we use variables we
don’t worry about what the address is and that’s’ why we don’t have to worry
about hexadecimal codes and we can focus on symbolic means.
In most of our programs we instantiate our variables
but how’s that values get stored in memory? For example- we have two variables
a and b which we’ll instantiate
int
a = 5;
int
b = 10;
Memory layout for these we’ll look something like
this:
If we consider the entire memory broken down into
blocks like above, we will find that the values of a and b we’ll occupy one of
these blocks. And as the data type of a and b is integer(int), integers are
32bit that means 4bytes and if our OS is 32bits, we will find that integers
occupy two of these blocks. So, we have 10 in one of the blocks and 5 in one of
the box. Each of these blocks have addresses and addresses can be from zero to
number of blocks our particular machine has or our program uses. One thing to
remember here is the program decides which block we’ll occupy which variable
values and we don’t have control over that. So, in short what is happening here
is, ‘a’ and ‘b’ symbolically refers to a memory location, 5 and 10 are the
values get stored in that locations and those lives in the memory spaces called
a or b, these locations has underlying addresses that is known by our C
program, we simply refers to those by ‘a’ or ‘b’. Now that we understand memory
layout let’s move on to pointer.
What
is a pointer?
We’ve just seen that when we initialized variable ‘a’,
it’s assigned in some location of the
memory and the C program will decide
which location it can be assigned and symbolically we refer to the
location as ‘a’.
But now we want to point to that location, what I
mean by it is- for example we can set another variable aPtr which is located in
completely different location in memory like below-
And aPtr holds the address of variable ‘a’
In short pointer is a
special type of variable because regular variable stores integers, doubles etc.
but pointers stores an address in memory. We can say a pointer points to
another address in memory.
Now that we know what a
pointer is, we have to know how to setup ‘aPtr’ to point to ‘a’.
C
implementation of pointer
it is just an one line
command like this:
int
*aPtr = &a;
Here int * means
pointer to an integer that means int specific what kind of variable this
pointer points to and * indicates that this is a pointer. The next thing here
is the & symbol before the variable ‘a’,’ &’ indicates that we want the
address of the variable not the value and we access the address of ‘a’ by that
symbol(&) and assign it to ‘aPtr’.
Type
of pointer variables
I have just given an
example of integer type pointer. But
what other type do pointer variables have? We can point to any type of variable
it can be primitive type(integer, double, character etc) as well as user
defined types(structure in C). Type of a pointer depends on what it points to.
But remember earlier I
have said addresses are just hexadecimal values, so now the question comes to
mind that if all addresses are just hexadecimal values and pointer just holds
the addresses, then why does it need to know what type of variable it points to?
See all different
variables occupies different amount of space in memory(i.e. int:4bytes,float:
4byes,char: 1byte) and pointer needs to know what type of variables it points
to so that we can do something called pointer arithmetic to those variables. I
will explain that on my next post.
Accessing
the value the address the pointers holds
It's also just one line
like this:
int
valueOfaPtr = *aPtr;
here we have a int type
variable ‘valueOfaPtr’ which is initialized by the pointer variable ‘*aPtr’
that points the variable ‘a’ whose value is 5. By ‘*aPtr’ we are getting the
value of the variable which address this (aPtr) variable is holding. So, now if
we print the variable ‘valueOfaPtr’ we we’ll see that it has the value 5.
The whole code:
First we have printed
‘a’ which has value 5 and after that we have printed ‘aPtr’ and it gave us a
hexadecimal value that is the address of variable ‘a’ ad after that we have
printed ‘*aPtr’ which give us the value 5 which is the value of ‘a’. Notice how
‘aPtr’ gives the address and ‘*aPtr’ gives the value, that’s why when we
initialized the variable ‘valueOfaPtr’ we have initialized with ‘*aPtr’ because
that is a normal variable we wanted to initialize that variable with value not
the address.
No comments:
Post a Comment