Saturday, November 5, 2011

Pointers in C for starters

Ask any new computer science student ,"What is the most dreaded topic in C language?". They will reply ,"Still having nightmares about pointers !!!". And I don't blame them. In fact pointers become even more dreaded as the learning curve is really steep for beginner.
So what makes pointers so "evil" ? I personally think its not the pointers but its lack of knowledge about memory that actually kills a new student. Hence I claim "Pointers are innocent ".



Pointers can be easily mastered by any new student, they just need to work with them. It is said that a good programmer will always avoid any type of programming error except that of pointers. Even the mightiest fail with pointers. So don't take it hard on your self , we all make mistakes (specially with pointers and then laugh about it later ).
So whats the deal about these strange looking creatures (pointers that is :P ). To understand it, lets look at the memory in detail.
If you want to visualize memory, its more like a huge set of boxes with a numbers on it. These boxes contain your data and the number on the box helps us getting to these boxes. These "numbers" are called "memory addresses". So everything you create in a C program will be stored in these boxes. every data type require a particular amount of boxes for storage. Like int requires 2 boxes each of 1 Byte in size (assuming int is 2 Byte long)

XYZ
123
100102104
here we see 3 int variables X,Y,Z. with values 1,2,3 and they are stored at memory locations 100,102,104 respectively. As we can see these int require 2 Byte of memory. Easy right?

Q: So where are the pointers?
Ans : In Simple words the pointers will store these Addresses. That's the whole purpose of a pointer,to store the address of other variable (int in this case).
Q: Can Pointers store values?
Ans : Well if the value is an address, YES.
Q : How do I create a pointer?
Ans :Well its very easy, you have to just mention that what data type the pointer will be "pointing".

int* ptr;

Read this statement from right to left. "ptr is a pointer to data type int".
so my ptr pointer will store the address of a int.
But i don't know where my variables are stored. :(
Yeah we know that that's why we use a special operator to get the address of your variable. this operator is "&" operator.
so i can write :-

ptr=&X;

read this statement from right to left. "Address of X gets assigned to pointer ptr". easy right?
Q: So &X gives me the address of X. Will &Y give me address of Y?
Ans : of course it will.
Q : But what is pointer itself?
Ans: Technically speaking pointer itself acts as an special integer that stores addresses.
Q: Pointer is Integer?
Ans : Yes it is.

Q: You mean the pointer itself is stored in memory too?
Ans: Of course like any other variable.

XYZPTR
123100
100102104200
You can see that the pointer itself is stored at address 200. and we can see that pointer contains the value 100 which is the address of variable X. and we say that ptr points to X.

Q: Fine. What can do with pointers now?
 Ans : actually u can access the values "pointed" by the pointers.

Q: I can? how?
Ans: its very easy , we use a special operator "*" called "value at pointer".
So if I say    *ptr  , I mean  "value at ptr".
pointer contains address 100, and the value AT 100 is =1 which is the value of X. (So basically we get the value of the data type we were pointing ).

Summary:-
  • Pointers points to data types.
  • Pointers only contain addresses.
  • "&;" operators is used to get the memory address of a data type.
  • "*" is used by pointers to read value it is pointing.
I hope it helps new C programmers.

1 comment: