Thread: Pointer & reference

  1. #1
    Registered User
    Join Date
    Oct 2001

    Pointer & reference

    What is the difference between using a pointer and using a reference?

    int K;
    int*p=&k ;
    (both *p=20; and ref=20; will assign the value 20 into the variable k)

    thanks for ur time and help.

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    You can get cleaner syntax with references, but they can't be re-assigned to 'point' elsewhere and must be initialised immeadiately.

  3. #3
    Registered User
    Join Date
    Oct 2001
    The "Cliff Notes" version of your question is a reference is a pointer but with less versatility. The reference is simpler and easier to use. A reference variable cannot be altered to refer to any variable except the one to which it was initialized.
    A reference passed to another function is preferred if possible because while the receiving function has a copy of the value in the address passed, it cannot change the value in the original function. Thus protecting the original value.
    References cannot be used for array notation.

  4. #4
    and references cannot be NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  4. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  5. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:08 AM