Difference between Pointers and Reference in C++

Discussion in 'C++' started by Shishir191, Jul 27, 2007.

  1. lead.smart34

    lead.smart34 New Member

    Joined:
    Feb 14, 2008
    Messages:
    77
    Likes Received:
    0
    Trophy Points:
    0
  2. crazytolearn57

    crazytolearn57 New Member

    Joined:
    Feb 14, 2008
    Messages:
    48
    Likes Received:
    0
    Trophy Points:
    0
  3. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    i agree to it
     
  4. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    Thanks to All.
     
  5. pavankumar.thati

    pavankumar.thati New Member

    Joined:
    Jul 9, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    there is a class Date and a function
    the function is non static

    Date& and *this is same in representation and they are explain plzzzzzzz.........

    Code:
    class Date
    {
                   Date& add_year(int);
                   Date& add_month(int);
                   Date& add_day(int);
    }
    int main()
    {
                  Date a();
                 a.add_year(1).add_month(2).add_day(2);
                     return 0;
    }
    Date& Date::add_year(int n)
    {
                      if(d==29&&m==2&&!leapyear(y+n)
                      {
                                  d=1;
                                   m=3;
                       }
                        y+=n;
                         return  *this;
    }
    Date& Date::add_month(int n)
    {
                     return *this;
    }
    Date& Date::add_day(int n)
    {
                   return *this;
    }
    Is this a valid one if then give me some explanation with examples.
    plzzzzzzzzzzzzz...........
     
    Last edited by a moderator: Jul 10, 2008
  6. Darrelljones

    Darrelljones New Member

    Joined:
    Jan 27, 2011
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization.
    A pointer can point to NULL while reference can never point to NULL
    You can't take the address of a reference like you can with pointers
    There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5).

    As a general rule,

    Use references in function parameters and return types to define attractive interfaces.
    Use pointers to implement algorithms and data structures.
     
  7. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Shishir pointed out in the very first post of this discussion that " Its not necessary to initialize the pointer at the time of declaration". I think its not mandatory but still we should always make sure that a pointer is initialized to NULL whenever it is declared because :

    Code:
    int main()
    {
        char *ptr;
        int retval = 0;
        retval = someFunc();
        if(retval)
            ptr = "I got some address";
        someOtherFunc(ptr);
    }
    
    void someOtherFunc(char *ptr)
    {
        if(ptr)
        {
            // use ptr
         }
    }
        
    
    Now in the above code snippet, suppose the value of 'retval' is 0 which ensures 'ptr' does not get address of string "I got some address" and inside the function 'someOtherFunc' the condition 'if(ptr)' may well be true as ptr may be holding some garbage address which is non-null and hence when we use ptr we may well observe a crash as through 'ptr' the program would try to access the garbage address
     
  8. kumarmannu

    kumarmannu Banned

    Joined:
    Feb 2, 2011
    Messages:
    51
    Likes Received:
    0
    Trophy Points:
    0
    1. A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization.
    2. A pointer can point to NULL while reference can never point to NULL
    3. You can't take the address of a reference like you can with pointers
    4. There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5).

    1) A pointer can be re-assigned:

    int x = 5;
    int y = 6;
    int *p;
    p = &x;
    p = &y;
    *p = 10;
    assert(x == 5);
    assert(y == 10);

    A reference cannot, and must be assigned at initialization:

    int x = 5;
    int y = 6;
    int &r = x;
    A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses -> to access it's members whereas a reference uses a .

    A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address the item it references.

    References cannot be stuffed into an array, whereas pointers can be (Mentioned by user @litb)
     
  9. kumarpyasa

    kumarpyasa Banned

    Joined:
    Nov 19, 2010
    Messages:
    10
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    seo
    Location:
    In Delhi
    Home Page:
    http://www.singaporetourpackage.com
    Hello dear
    Pointers and references are the different
    1. pointers use the “*” and “->” operators and references use “.“
    2. A pointer can be re-assigned any number of times while a reference can not be reassigned after
    initialization.
    3.A pointer can point to NULL while reference can never point to NULL
    4. You can't take the address of a reference like you can with pointers
    5. There's no "reference arithmetics" (but you can take the address of an object pointed by a
    reference and do pointer arithmetics on it as in &obj + 5).
     
  10. alexsmth114

    alexsmth114 New Member

    Joined:
    Mar 7, 2011
    Messages:
    19
    Likes Received:
    1
    Trophy Points:
    0
    Really nice tutorial, thanks for sharing!!..
     
  11. alexsmth114

    alexsmth114 New Member

    Joined:
    Mar 7, 2011
    Messages:
    19
    Likes Received:
    1
    Trophy Points:
    0
    Really nice comparison, useful post, keep them coming!!..
     
  12. msdnguide

    msdnguide New Member

    Joined:
    May 14, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    msdnguide
    Location:
    Delhi
    Home Page:
    http://www.msdnguide.info/
    Reference is just an alias. Like a different name to same variable where as Pointer is the actual memory location where the variable is stored
     
  13. sachin.magdum

    sachin.magdum New Member

    Joined:
    Jan 29, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Little curious about fourth difference. Can we really not create reference to reference?

    int obj;
    int & ref = obj;
    int & ref_2 = ref; // although equivalent to int & ref_2 = obj;
    // I agree that we do not have some thing like int & & ref_to_ref = ref;

    ------------------------------------------------------
    1. There exists void pointers, but no void references.
    2. Pointer is a variable, that has its own address. References are alias to some other variable, references do not have their own address.
     
  14. sura

    sura Banned

    Joined:
    Aug 4, 2011
    Messages:
    47
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    India,Tamil Nadu.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice