Pointer & reference

This is a discussion on Pointer & reference within the C++ Programming forums, part of the General Programming Boards category; What is the difference between using a pointer and using a reference? e.g.: int K; int*p=&k ; &ref=k; (both *p=20; ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Pointer & reference

    What is the difference between using a pointer and using a reference?
    e.g.:

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

    thanks for ur time and help.
    ~ayesha~

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

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    28
    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.
    rc7j

  4. #4
    Unregistered
    Guest
    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, 03:08 AM

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