Sunday 8 December 2013

Pointers


#include <iostream>
using namespace std;

int main ()
{
  int a = 5,b = 15;
  int *p1, *p2;

  p1 = &a;             // p1 = address of firstvalue
  p2 = &b;             // p2 = address of secondvalue          
  cout<<*p1<<endl<<a<<endl;
  cout<<"*p2<<b<<endl;
  cout<<&a<<p1<<endl;
  cout<<&b<<p2<<endl;
 
  
}


In this,we will discuss pointers......!
So,first we known what are pointers.....!!
Pointers:-
Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool.


In this program,we will take two int,a and b and two pointers,p1 and p2 of int type.....!!
When we assign p1=&a,we will giving p1 address of a and same case with p2.....!!
and then we will printing out *p1,which will print the value which we give to a .....!!
How's this possible.......Actually,p1 is pointing to a(not to its value) and *p1 means what the value of a is........!!

And if we print &a it will print the address of a,and if we print p1 it will also printing the address(These two addresses are same) and same case with p2 and b......!!

I think you got it.........!!!
Feel free to give suggestions in comment box.......! 

0 comments: