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

Go4Expert Founder
27Jul2007,18:31   #2
shabbir's Avatar
Nice differences.
Go4Expert Member
27Jul2007,18:33   #3
Shishir191's Avatar
Thanks.
Go4Expert Member
14Aug2007,14:09   #4
kush_2207's Avatar
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 ?
Go4Expert Founder
14Aug2007,15:36   #5
shabbir's Avatar
In Java or C# everything is reference and there is nothing known as pointer.
Go4Expert Member
14Aug2007,18:54   #6
kush_2207's Avatar
Thank you sir.
Go4Expert Member
15Aug2007,09:14   #7
kaustubh's Avatar
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.
Newbie Member
8Sep2007,13:37   #8
shal's Avatar
nice one
Newbie Member
10Sep2007,16:26   #9
zaka_d's Avatar
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.
Team Leader
10Sep2007,22:00   #10
DaWei's Avatar
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.