POINTERS


What are Pointers  & why we use Pointers ... ?

This is the question which comes first into our minds  when we hear this word  POINTERS
.
To understand this idea , we should go back and learn about different types of  variables & Data types . Now another question arises which is about Data types and variables.
What is a data type ?
Data type is actually represents a set of unique values along with a set of operations that are allowed for application on those unique values.
Data types are of three types:
simple                                    structured                            pointers
we have studied only simple data type and operations acting upon them such as + , - , * , / and % (% is not applicable to double data type)
1: simple data type
Simple Data type can be further subdivided into following
2 : Structured data type:
Structured data type is related  to object oriented programming. And we have not studied it yet.
Now I will discuss pointers data type.
Pointer data type:
In our daily life , we illustrates the position of something by giving directions , addresses  or point out that thing with the help of fingers or hand signals.

Lets take an example from our daily life. Assume our first day in  university , we wanted to reach to Computer science departments , but we did not know the exact location of department. First when  we entered into university through main gate , we were totally unknown from university . then we asked a student , where is the CS & IT department ?
he / she told us that if we will go straight from main gate and  then cross the fountain and continue walking straight then we will reach in front of  central library, then turn left and after some distance we will see CS & IT dept . we followed that path and finally we reached at our destination . that person had actually given us the address of our dept. Think , what would happened if  that person gives us wrong direction ?

surely we would go on wrong path and we would never reach to our destination. Now remember this story in your mind because  later we will use the same analogy.
Here is a point to be remembered that the person from which we asked about CS & IT department, he gives us only direction and address of department . he did not take us to the department but only show us the address of department…..
Now come to our real problem , which is not other then  pointers…!
In programming , some times we want to access the original location of some variable . then surely we should have the address of that variable.
In c++  programming language , we can get the address of our desired variable by using two ways . by using references and by using pointers . actually these two ways has same functionality at some points but they also differ at some other points.
Reviewing the functions portion , we know that we can easily change the values of  some variable declared  inside main function by calling another function which takes the reference perimeters like this
#include<iostream>
using namespace std;
void function (int & num1 , int & num2);

int main()
{
int first_number , second_number ;
first_number = 10;
second_number = 15;
cout<<" inside main , first number = "<<first_number<<" and  second number = "
<<second_number<<endl;
cout<<"now another function will be called …."<<endl;

function (first_number , second_number);

cout<<" inside main , after calling the other function  "
<<"first number = "<<first_number<<" and  second number = "
<<second_number<<endl;
return 0;
}
void function (int & num1 , int & num2)
{
num1 = 200;
num2 = 300;
cout<<"on exiting the function  , num1 = "<<num1<<" and  num2 =  "<<num2
<<endl;
}

Now what the above program will do? In above , void function (int & num1 , int & num2) shows that this function takes two reference perimeters . now look at the function call ,

function (first_number , second_number);

although it is call by reference but it still looks like Call by Value , in which the copied values are passed and the original values remains unchanged.
when we call another function inside main function or in any function  , then we supplies the values(arguments) to that function. If  the called function has ampersand & infront of perimeter name , then all this process of calling is called Call by Reference.in this process , compiler automatically gets the address of arguments . if we modify the values then  the original values will also be changed. But Call by Refence is a dangerous process because sometimes it chages the original values.

POINTERS are used for the same purpose . they are actually a data type. And it contains only and only memory addresses in it . we can not assign integers values to pointers .
When we use call by reference then compiler automatically gets the addresses of arguments using ampersand &.
But when we want to save the memory locations separately , then we will need a special type of variables to hold memory addresses. That special type is POINTERS. Once we save the address of a variable , then we can easily use that address many times.
Now we will look how to declare POINTERS….
int * ptr ;
to declare a pointer , first write a data type , then use * and finally write the pointer name.
the above declaration will be read as    “   a pointer to integer type “
second step is to initialize the pointer , and it is an important step. Always initialize the pointers when they are declared .As mentioned  earliar , pointers will only contains memory address , which is a hexadecimal address.
int sum = 40;
int * sumPtr = & sum;
above two lines actually declares an  integer variable sum and a pointer to that variable.
sumPtr        is the pointer which holds the address of sum variable , using & ampersand.
& is used to obtain address of sum .
Now recall our previous analogy about reaching the CS & IT department , the person who had given us the direction and address of department , was actually behaving like a pointer.
He gave us only address of department but he did not showed us the actual department building which was present at that location.
Now , I will discuss how to use pointers to reach the contents of a variable to which the pointer points.first of all we need to dereference the pointer. Now I have introduced a new term Dereference , but don not get confused. I will explain it soon….
First look at the following c++ statement
* ptr
Above statement shows a * and name of pointer variable. Here note one thing that data type is not present here. This step is known as dereferencing a pointer. Now I will discuss the purpose of this step…
When you write the following
int * sumPtr = & sum;
then you are declaring and initializing a pointer i.e. assigning address to a pointer.now look again at following dereferencing statement
* sumPtr = 400;
Actually when we dereference a pointer , then it returns (synonym) the name of the variable to which it points i.e. variable name present at that address .
The above statement is equivalent to
 sum = 400;
finally , remember this
sumPtr = (memory address of sum)
and
*sumPtr =(contents of sum)

Now I am writing a program in which pointers are used ,

#include<iostream>
using namespace std;
void function (int * num1 , int * num2);
int main()
{
int first_number , second_number ;
first_number = 10;
second_number = 15;
cout<<" inside main , first number = "<<first_number<<" and  second number = "
<<second_number<<endl<<endl;
cout<<"now another function will be called and addresses will be passed."<<endl<<endl;

function (&first_number ,& second_number);

cout<<" inside main , after calling the other function  "<<endl
<<"first number = "<<first_number<<" and  second number = "
<<second_number<<endl;
return 0;
}
void function (int * num1 , int * num2)
{
*num1 = 200;
*num2 = 300;
cout<<"on exiting the function  , *num1 = "<<*num1<<" and  *num2 =  "<<*num2
<<endl<<endl;
}

Look at the function call , we use & to provide addresses as arguments. And we know , when addresses are passed then special type of variables should be declared in funtion perimeters to hold addresses . that special type is POINTERS. So in funcyion heading
void function (int * num1 , int * num2)
two special type perimeters are declared. These are pointers and they will hold the addresses of first_number and second_number.
Now once we have the address , then we can easily modify the contents of first_number and second_number . for this perpose , I have dereferenced the pointers .Dereferencing gives the synonym (variable name ) and then reassigns new values. After function completes its task , control shifts back to main function. In main function , values of   first_number and second_number are changed.
In the end , consider the difference in Call by Reference and pointers:
A function call using references will look like this

function (first_number , second_number);

and function call usig pointers will look like this

function (&first_number ,& second_number);

Here I have tried my best to make the concept of pointers clear , I have used simple words and some examples. But every thing needs  improvement…
Once you understand the concept , then read the text book because pointers are explained there in details.
I have only covered here the basics of pointers , next I will discuss pointers in depth and will try to answer the questions about pointers as reference perimeters and difference between pointers call by value and call by reference….
If you find some confussion about what I have discussed here , then please through E-mail or in the class , tell me about your confussion.