A small introduction to POINTERS.....!!Pointers are generally the most feared part in a programming language. The only concept which many programmers doesnt seem to get. Well just get over the fact that pointers are somrthing terrifying build only to make prgrammers like us have horrifying nighmares....!! Heres the truth about pointers....!!
POINTERS ARE NOTHING MORE THAN A VARIABLE...!!
but the twist here is that they are a special kind of variables that stores special kind of info and that special info is the ADDRESS of another variable.
See just like an int variable can store only int values and a float can store only float values, same is the case with the pointers. An int pointer stores the address of an integer variable only and not a float variable.
So in 1 line a pointer is :
SIMPLY A VRAIABLE THAT STORES ADDRESSES OF ANOTHER VARIABLE OF SAME DATA TYPE.
DEFINING A POINTER
defining a pointer is a little different than defining variables.
int* x; /*we just declared an integer pointer which means that it vil only store addresses of integer variables.*/
dont be confused with the star (*)...u can also do lyk
int *x or int * x...its all d same....!!
Similarly to create a float or a char pointer just change the data type
now store something in the pointer.
REMEMBER : The variable that the pointer is pointing to is called a "POINTEE".
pointer = &pointee; //Now wat the hell is "&" for.. >:)
this is known as "address of" operator it takes a variable and returns its address.
for eg: &pointee will give you the address of pointee, remember that pointers only store addresses so if we want pointer to point to pointee we will give the address of pointee
So pointer is pointing to pointee now so wat.....??
Now we can access pointee through a dereference operater(*)...
int pointee = 10;
vil give 10.
Suppose address of pointee is 1000 and value is 10.
So pointer vil store 1000 as its value and when *pointer is done, it will go to the address conatined by pointer which is 1000 as of now and vill access d value storesd at that address.
So *pointer is now the same as pointee.
doing *pointer = 20;
or pointee = 20 //is the same thing
Suppose there is another int pointer
int* another_pointer ;
now we want this new pointer to point to the same location that pointer is pointing to.
so we vil do lyk:
another_pointer = pointer; //another pointer vil now also point to pointee;
So now the final programme to illustrate the concepts:
int pointee = 10;
int *pointer1, *pointer2; //u cant do lyk int* pointer1,pointer2,here pointer2 vil be pointer1 = &pointee; //a simple int
cout<<pointer1; //address of pointee
cout<<*pointer1; //value of pointee i.e 10
pointer2 = pointer1; //pointer2 now points also points to pointee
*pointer2 = 20; //value of pointee chngd to 20
cout<<pointer2; //address of pointee
cout<<pointer1; //same as above...address of pointee
cout<<*pointer2; //value of pointee which is now 20
cout<<*pointer1; //value os pointee which is now 20
I hope this vil settle all the dust about baiscs of POINTERS and now we can start understandin the intricate details about pointers and their GOTCAHS....!!
This is just a very breif intro to POINTERS and Theres still a lot to learn about pointers so keep writing code and experimenting....!!