Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Difference between Pointers and Reference in C++ (http://www.go4expert.com/articles/difference-pointers-reference-cpp-t5492/)

Shishir191 27Jul2007 17:36

Difference between Pointers and Reference in C++
 
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.

shabbir 27Jul2007 18:31

Re: Difference between Pointers and Reference in C++
 
Nice differences.

Shishir191 27Jul2007 18:33

Re: Difference between Pointers and Reference in C++
 
Thanks.

kush_2207 14Aug2007 14:09

Re: Difference between Pointers and Reference in C++
 
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 14Aug2007 15:36

Re: Difference between Pointers and Reference in C++
 
In Java or C# everything is reference and there is nothing known as pointer.

kush_2207 14Aug2007 18:54

Re: Difference between Pointers and Reference in C++
 
Thank you sir.

kaustubh 15Aug2007 09:14

Re: Difference between Pointers and Reference in C++
 
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 8Sep2007 13:37

Re: Difference between Pointers and Reference in C++
 
nice one

zaka_d 10Sep2007 16:26

Re: Difference between Pointers and Reference in C++
 
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 10Sep2007 22:00

Re: Difference between Pointers and Reference in C++
 
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.


All times are GMT +5.5. The time now is 12:56.