Thread: Pointer question.

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Pointer question.

    I am still trying to get my head around pointers. I cant really see what they do that reference does not. (Apart from arrays)

    So, here is my question today:
    When you create a pointer, is the address to it useful for anything? Are there some cases where you would want to take an address of a variable and do something with it(Not reference it)?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not very much... the point of the address is to have it so you can dereference the value later.
    What are pointers good for, you ask?

    Optional parameters, obviously, where a non-const value is required.
    Keeping track of a position somewhere in memory and (optionally) incrementing or walking it (like arrays).
    In classes where you dynamically need to bind an object/value/whatever, where you cannot bind this object/variable at construction.
    Can be reassigned after they've been defined. So if you have code that must deal with multiple objects depending on conditions, the pointer can be assigned to the object and you can have a single path of code (can't be done with references since they must be bound as they are defined and that would mean defining them inside the if conditions, which must they are destructed just as quick).

    Those are a few examples.
    Last edited by Elysia; 02-27-2008 at 09:48 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    References cannot be re-assigned, but pointers can:
    Code:
    int  i1 = 666;
    int  i2 = 123;
    int& r1;        // Error, must initialize when created
    int& r2 = i1;   // OK
    r2 = i2;        // OK, but doesn't re-assign the reference, it just assigns a new value to it.
    int* p1;        // OK, but bad practice.
    int* p2 = NULL; // OK
    p2 = &i1;       // OK
    Last edited by cpjust; 02-27-2008 at 09:08 PM. Reason: Fixed a comment.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I cant really see what they do that reference does not.
    Pointers can also be null, so you can use a pointer to indicate that a particular parameter or variable is optional.

  5. #5
    coder
    Join Date
    Feb 2008
    Posts
    127
    sometimes using reference "hide" the fact you are passing an address:
    this may make the code unclear and hard to debug

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    sometimes using reference "hide" the fact you are passing an address:
    this may make the code unclear and hard to debug
    The context of the code should more than clarify what is going on.
    Last edited by VirtualAce; 02-27-2008 at 07:14 PM.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    As others have said, you cannot change what a reference points to, as you can for a pointer.
    Pointers also have the advantage of being nullable -- that is there is a special state for not pointing to anything.

    @cpjust
    Code:
    r2 = i2;        // Error, cannot re-assign a reference.
    While it is true, that references cannot be reassigned, that line is still valid code. It assigns i1 the value 123.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by King Mir View Post
    @cpjust
    Code:
    r2 = i2;        // Error, cannot re-assign a reference.
    While it is true, that references cannot be reassigned, that line is still valid code. It assigns i1 the value 123.
    Oops, that's right. Well you get the idea anyways...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM