Question about pointers.

This is a discussion on Question about pointers. within the C Programming forums, part of the General Programming Boards category; I'm having a hard time using pointers because I don't understand them that well. When is it correct to use ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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 06: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
    22,269
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:12 AM
  2. Question about Pointers
    By Livnat in forum C Programming
    Replies: 6
    Last Post: 09-07-2008, 01:42 AM
  3. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 01:54 PM
  4. Question about pointers.
    By RealityFusion in forum C++ Programming
    Replies: 12
    Last Post: 10-15-2005, 01:28 AM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM

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