Thread: main difference between pointer and reference and how to use pointers in c++

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    main difference between pointer and reference and how to use pointers in c++

    i'm still unclear between the difference between using pointer and a reference


    I understood the concept of pointers in c in the class i took last year

    and that was to change the actual value stored in the memory address
    Code:
    
     void change_a(int a*){
    
            a=6;
    }
    
    int main(){
      
                 int a=5;
                 change_a(&a);
    
    
    }

    but in c++ I've been using references in all my assignments because I don't know how to correctly use pointers in c++ I may have missed a class but I'm on spring break and would like to clear things up

    so in c++

    in my assignments I would call it like this
    Code:
    void change_a(int &a){
    
              a=6;
    
    }
    
    int main(){
    
    int a=5;
    change_a(a);
    }
    so does this change the value in the address or does it make another copy of a in my c++ code and stores 6 in that copy

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This is the view of a C programmer learning C++; it might be wrong.

    pointer vs. reference

    references (are sometimes called variable alias) and are NOT able to be NULL or change value.

    pointer hold the address to another variable and are allowed to be NULL and can change value to point to another variable.

    In C++, it is always better to use references instead of pointers (when that use is possible).

    In C++, it is usually better to use smart pointer class (class(es) that can be used in place of regular pointers) instead of normal pointers.

    Tim S.
    Last edited by stahta01; 03-09-2013 at 07:31 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I suspect you haven't understood pointers particularly well in C, since (almost) everything you can do with pointers in C is done in exactly the same way in C++. The only technical difference is that, in C, void pointers can be implicitly converted to any other pointer type in C, but the type conversion has to be explicitly done in C++. The practical differences are a bit larger: a lot of techniques in C involving significant usage of pointers are considered very poor practice in C++, since C++ provides viable alternatives (containers, so-called "smart pointers", etc) that are considered less error prone.



    Practically, a reference is just an alternative name for a variable. Given a pointer p, then *p is a reference to whatever p points at (although this does not mean the compiler manages reference using pointers behind the scenes). However, the value of a pointer p can change, and changing the value of p changes what *p refers to. However, a reference cannot be changed in that way - assignment to a reference changes the variable itself.
    Code:
    int main()
    {
          int a, b;
    
          int *p = &a;    //  p is a pointer to a, so *p is a reference to a
    
          int &r = a;       //   r is a reference to a
    
          *p = 42;         // changes the value of a
          r = 43;           // changes the value of a
    
          p = &b;          //   changes p so it points at b.   *p is a now a reference to b
      
          *p = 44;        // changes the value of b
    
          r = b;            //   this changes the value of a (not b).    
    }
    Another difference between pointers and references is that a pointer can have a zero (also called NULL) value, indicating it does not point at a valid object (which means that the act of computing *p gives undefined behaviour). A null reference cannot be created (any code statement that creates a null reference, such as by dereferencing a NULL pointer, has undefined behaviour). So;
    Code:
    int main()
    {
          int a;
          int *p = NULL;
    
         int &r;    //   This will trigger a compiler error, since a reference must be initialised to refer to something.
    
         int &r2 = *p;    // this has undefined behaviour since p is NULL
    
         if (p == NULL)
          {
               p = &a;    // we are allowed to reseat p.   Note that this does NOT affect r2 above.
               *p = 42;   // this changes the value of a.
          }
    
         r2 = 43;    //   since the creation of r2 gave undefined behaviour, this does too.   Typical symptom is a program crash.
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between Reference and Pointer?
    By DarkMage530 in forum C Programming
    Replies: 10
    Last Post: 09-20-2011, 11:03 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Undefined Reference to Main Error
    By rrc55 in forum C Programming
    Replies: 10
    Last Post: 07-15-2009, 01:32 PM
  4. Difference in initialising main
    By nightingale in forum C Programming
    Replies: 16
    Last Post: 07-27-2003, 11:32 AM
  5. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM