Reference and Pointers

This is a discussion on Reference and Pointers within the C++ Programming forums, part of the General Programming Boards category; I was going through the Faq- What's the difference between... > '&' and '*' (C++)? It says: References cannot have ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    41

    Reference and Pointers

    I was going through the Faq- What's the difference between... > '&' and '*' (C++)?

    It says:
    References cannot have operations performed on them since they are not variables

    but if I have something like :
    int i = 1;
    int& r = i;

    then I can do
    r++; to increment r.

    So what does the above statement mean?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It increments the value referred to by the reference (the value of i). It does not increment the reference itself.

    If you did the same with the pointer, the pointer would point to a new value:
    Code:
    int i = 1;
    int* p = i;
    p++; // increment the pointer (technically undefined behavior)

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by Shal
    I was going through the Faq- What's the difference between... > '&' and '*' (C++)?

    It says:
    References cannot have operations performed on them since they are not variables

    but if I have something like :
    int i = 1;
    int& r = i;

    then I can do
    r++; to increment r.

    So what does the above statement mean?
    Think of a reference as simply an alias or nickname for another existing object in your program. Pointers, on the other hand, are objects themselves, with their own place in memory, and a value.

    Since a reference isn't an object (a reference doesn't really physically 'exist' anywhere), then there's not really any way you can perform an operation on a reference. Any operation you perform on the nickname/alias is actually performed on the referred object instead.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    In general, references are easier than pointers to learn, but references cannot be NULL, where as a pointer can. You cannot assign a reference to nothing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 09:14 AM
  2. Passing pointers by reference
    By JOCAAN in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 10:02 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Passing Pointers By Reference
    By hern in forum C Programming
    Replies: 15
    Last Post: 07-29-2003, 11:43 AM
  5. Pointers and reference passing
    By Denis Itchy in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2002, 12:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21