Thread: Question about pointers.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    61

    Question about pointers.

    I'm having a hard time using pointers because I don't understand them that well. When is it correct to use the * and & operator and what do they do?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What have you read about them?I admit some explanations are better than others. Don't give up though.

    The & is the address of operator, which fetches the address of some variable. Pointers use this address as a value.

    The * is the dereference operator, which fetches the object stored in the pointer's value. In conjunction with this operator you can change the pointed to object. This is useful because you can modify things that weren't given a name, like memory returned from malloc()/calloc()/realloc(). The only time it is not okay to use the * operator is when the pointer is invalid, meaning it doesn't point to a usable address. Such pointers could have the value NULL. It is good practice to NULL pointers that shouldn't be used anymore because otherwise you just don't know and run the risk of invoking undefined behavior.
    Last edited by whiteflags; 08-31-2012 at 05:36 PM.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What i would also like to add is that & and * operators kill each other when they are used adjacent to each other(no matter which operator comes first).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    What i would also like to add is that & and * operators kill each other when they are used adjacent to each other(no matter which operator comes first).
    Not quite, e.g., if x is an int, then &*x is invalid even though *&x is valid. On the other hand, if p is a pointer to an object, then &*p results in p, except that the operations are not actually evaluated and the result is not an lvalue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by laserlight View Post
    Not quite, e.g., if x is an int, then &*x is invalid even though *&x is valid. On the other hand, if p is a pointer to an object, then &*p results in p, except that the operations are not actually evaluated and the result is not an lvalue.
    Totally true.Thanks for the complementary

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Question about Pointers
    By Livnat in forum C Programming
    Replies: 6
    Last Post: 09-07-2008, 12:42 AM
  3. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 12:54 PM
  4. Question about pointers.
    By RealityFusion in forum C++ Programming
    Replies: 12
    Last Post: 10-15-2005, 12:28 AM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM