Skip to main content

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.

Popular posts from this blog

Find nth Prime Number in C++

c++ program to find prime numbers: The problem of finding prime number can be solved by checking all numbers, testing them for prime and then moving ahead. If you want to calculate nth prime. Then this can be done in a brutal way by checking the number one by one. This may sound odd, by there is no easy way then this  for prime numbers (Well Actually there are like Pollard's Rho Algorithm, Number Sieves or Shor's Quantum Algorithm, but we are talking about the one that most people may understand easily). There may be way to pre-calculate the prime numbers but that again is not sufficient. So how can we use c++ to create a program to find prime numbers.

Overloading Unary Operator in C++

In this Example I will demonstrate overloading a unary operator. The overloading is done in similar way as we did for binary operator. In this example I will be using a Vector Class. The operator we are going to over load is the unary "~" (tilde) operator. although you can use any unary overloadable operator. Method: In this example the overloaded operator will be the part of the class i.e. the over loaded definition will be a member function. (As we know overloading can be done using non member functions too). So the unary operator will not have any arguments in it. We will overload ~ operator so that it will reflect the vector so that its z component becomes x component and vice versa. So input vector will be 2i+3j+5k and its output vector will be 5i+3j+2k. although you can do anything you like. Code: # include < iostream > using namespace std ; class vector { int x , y , z ;      public : vector ( ) { x = y = z = 0 ; ...