Difference between Pointers and Reference in C++

Shishir191's Avatar author of Difference between Pointers and Reference in C++
This is an article on Difference between Pointers and Reference in C++ in C++.
Rated 3.00 By 1 users
Pointers and Reference looks similar but there are some difference between both of them.

POINTER

  1. Its not necessary to initialize the pointer at the time of declaration. Like
    Code:
       
    int a = 10;
    int *P = &a; //It is not necessary
    Another way is :
    Code:
    int a  = 10;
    int *P;
    P =  &a;
  2. You can create the array of Pointer.
  3. You can assign NULL to the pointer like

    Code:
    int *P = NULL; //Valid
  4. You can use pointer to pointer.

REFERENCE

  1. Its necessary to initialize the Reference at the time of declaration. Like
    Code:
    int &a = 10;
    int &a;   //Error here but not in case of Pointer.
  2. You can not create the Array of reference.
  3. You can not assign NULL to the reference like

    Code:
    int &a = NULL; //Error
  4. You can not use reference to reference.
leila like this
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nice differences.
Shishir191's Avatar, Join Date: Jul 2007
Go4Expert Member
Thanks.
kush_2207's Avatar, Join Date: Jun 2007
Go4Expert Member
good one.....but what would you say if i write in .NET or even in JAVA
FileStream fs; // (or using any other class)
What is FileStream -> a pointer or a reference ?
Can you elaborate it for other platforms ?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
In Java or C# everything is reference and there is nothing known as pointer.
kush_2207's Avatar, Join Date: Jun 2007
Go4Expert Member
Thank you sir.
kaustubh's Avatar, Join Date: Aug 2007
Go4Expert Member
Reference is the other name given to the same variable. Pointer is new variable created which can contain address of another variable.

Try an experiment

Code:
# include<iostream>
using namespace std;
int main()
{

int i = 90; // variable
int &j = i;  // reference 
int *P;   //  pointer
P =&i;

cout<<endl<< "address of variable :i" <<&i;

cout<< endl << "address of reference: j" <<&j;
cout<<endl<<"address of pointer : P"<<&P;
return 0;
}
OUTPUT:
 address of variable i: i0012FEDC
address of reference j: 0012FEDC
address of pointer p: 0012FEE0
so you see the address of i and reference j is same , address of pointer P is different.
shal's Avatar, Join Date: Sep 2007
Newbie Member
nice one
zaka_d's Avatar, Join Date: Sep 2007
Newbie Member
In C++ a reference variable is internally implemented as a constant pointer, and that is why it is necessary to initiallize a reference variable during declaration... as it is treated as a constant variable (pointer) internally.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
That is incorrect, zaka. A reference is a second name (alias) for an object. You can't name something that doesn't exist, therefore you have to specify the object being aliased when you declare the reference. When a reference is passed as an argument, a copy of the objects address is passed. This is one fewer levels of indirection than when a pointer to the object is passed.