Need a breakdown on pointers

Discussion in 'Mac Programming' started by NSG10, Sep 2, 2010.

  1. macrumors member

    Aug 26, 2010
    I need a simple explanation on how to understand pointers in a computer language. How you you guys think of pointers and how you understood them?
  2. macrumors 6502a

    Apr 1, 2007
  3. macrumors 68000

    Mar 29, 2009
    What part are you having the most problems with?
  4. thread starter macrumors member

    Aug 26, 2010
    Basically, I just need an understanding in general, so I can get a twist on it.
  5. macrumors 68040


    Jan 10, 2005
    Dallas, TX
    A pointer stores an address in memory. This is the most important thing to understand. Generally pointers have a type associated that indicates what you will find in the memory location they contain. A pointer does not contain the value in memory it points to, only the address.

    It's important to remember that the pointer type is different from other types. I'll assume C-family, as that's likely what you're learning. There are special operators that operate on/with pointers:
    Unary &
    This operator returns the address of its operand, which can be stored in a pointer of the type of the operand. This allows you to get the address of a local variable, for instance, and pass it to a function that takes a pointer.

    Unary *
    this operator takes a pointer operand, and evaluates to the value stored in memory at the location stored in this pointer. The type of the expression will be the flavor of the pointer. This allows access to whatever the value is in the memory we are pointing to.

    Binary []
    the first operand is a pointer, which appears before the [. the second operand is an offset, which appears between the brackets. This is similar to *, and if the second operand is 0, it will return exactly what * would. The second operand is multiplied by the size of the flavor of the pointer, and the resulting value is added to the first operand. This yields a new address, and the value stored in the memory at this address is the result, with the type matching the flavor of the first operand.

    Binary +/-, Unary ++/--
    you can add or subtract from a pointer. This is less common than other operators. The numerical operand (1 or -1 for ++ and --) is multiplied by the size of the type of the flavor of the pointer operand. The result is added to the address stored in the pointer, and this evaluates to the new address, with the same type as the pointer operand.

    Pointers are really just the way to store where you stashed something so you can find it later. The address of a house is a common example. The pointer/address to the house is not the house, it's just where to find it. What's at that address isn't fixed, either. A new house could be there the next time you visit. It just tells you where to look. You can have as many copies of the address as you want.

    I would draw yourself some pictures, and write a few test programs using the %p format specifier for printf or NSLog judiciously to print your pointers.

    Come back, ask questions. It will click.

  6. macrumors 604

    Aug 9, 2009
    It helps a lot to understand how computer memory is organized, and what an address means. Here's a nice explanation, with pictures:

    The whole series is pretty good, too.
  7. macrumors 604

    Jul 29, 2003
    Silicon Valley
    Think of a very long street with millions of houses along it. You can put a number in a house (that's data), on the front of each house is a unique sequential number for that street (that's the address), you can take a number from within, say, house A and use that number as a street address to find house B (that makes house A a pointer).

    But in the end, they're all just numbers, and places to stick numbers.
  8. macrumors 68000

    Mar 29, 2009

Share This Page