Thread: FAQ Pointer & reference (C++)

  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
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. Pass by pointer, reference, value.
    By Lithorien in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2005, 10:03 AM