I started learning Object Oriented Programming and I am feeling great. As if I am gifted with a powerful weapon, a great skill. Indeed it is. There are several concepts easy, difficult and confusing. Pointers is one such. Pointers will be confusing if you do not devote you full concentration.
What are Pointers good at?
They Point!
They point at things like Objects and Variables. They store the address where the Object or Variable is stored hence point at them. Lets understand what Pointers are and how they can be used using the example of a Pointer pointing at a Variable.
Since C++ is a good language to learn OOP I will be using it to explain (in fact this is how I learnt about pointers properly).
Lets first a declare a Variable of the type integer.
int intVar;
intVar = 5;
So a variable by the name “intVar” with the value 5 is declared. Now I will show how to declare a Pointer that points at this Variable.
Declaring a Pointer
int* intPoint=0;
intPoint = &intVar;
Here * represents that intPoint is the name of a pointer. It is first initialised to 0. That means it is now pointing to a address 0. If not initialized then it will be a wild pointer which is dangerous. Trying to access it’s value will give “Segmentation Fault”. So in the next line you assign it to the address of the variable “intVar”. &intVar represents the address of intVar. So now this pointer can be used to access the Variable’s address and also it value.
For accessing the variables value we use *intPoint.
So if you put *intPoint = 9 it means we are reassigning the value of intVar as 9.
So now
cout<<intVar<<endl;
will give 9 as the output.
Note that the data type of the Pointer is int * which represents that the pointer will store an address of a variable whose value will be an integer.
A Variable has a value and an address. A pointer also has an address of its own. It also stores a value which is the address of a Variable, hence points at its Value. *Pointer means the value at Pointer. Value at operator (*) is different from the one used in declaring the pointer. The value at operator (*) is used to retrive the value stored at an address. &Variable means address of Variable. &Pointer means address of Pointer.
In programming Var = 5 is semantically correct while 5 = Var is not. Similarly &Var = Pointer is semantically wrong.
So &intPoint and &(&intVar) won’t represent the same thing. The second one represents the address of the address of a Variable, which is meaningless.
Many Variables can have the same value, but One Variable cannot have many values.
Similarly, Many Pointers can point at the same address (of a variable), but One Pointer cannot point at many addresses (of variables).
*Variable is invalid, because this means value stored at Variable. Variable itself represents the value and &Variable it’s address. *Variable has no meaning and should give a compile time error.
Whereas *Pointer is valid, because this means value stored at Pointer or value stored at address of a Variable.
The valid way to assign to a Pointer
pointer = &variable;
This is valid always.
*pointer = value;
This is valid only if the pointer is pointing to some valid address else during runtime “Segmentation Fault” will be displayed.
This post was about what pointers are and how they are declared and used. Why should we use them should be your next question. Because if we use pointers to access only the value of a variable via its address, then it is unnecessary as we can just use the variable! Well, pointers have a lot of other usage that makes it essential.




one main usage i find intresting is pointers of all data types are of same size. So i can work on pointers without properly definfing the data types before defining the pointer itself
[...] Poking around with Pointers [...]