1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Difference between Pointers and Reference in C++

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

  1. lead.smart34

    lead.smart34 New Member

    good info
     
  2. crazytolearn57

    crazytolearn57 New Member

  3. aisha.ansari84

    aisha.ansari84 New Member

    i agree to it
     
  4. Shishir191

    Shishir191 New Member

    Thanks to All.
     
  5. pavankumar.thati

    pavankumar.thati New Member

    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

    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

    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

    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

    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

    Really nice tutorial, thanks for sharing!!..
     
  11. alexsmth114

    alexsmth114 New Member

    Really nice comparison, useful post, keep them coming!!..
     
  12. msdnguide

    msdnguide New Member

    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

    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

    nice.
     

Share This Page