PDA

View Full Version : Need a breakdown on pointers




NSG10
Sep 2, 2010, 06:16 PM
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?



Hansr
Sep 2, 2010, 06:34 PM
Pointers are pretty easy to understand and pretty easy to screw up too ;)

http://www.cplusplus.com/doc/tutorial/pointers/

robvas
Sep 2, 2010, 06:39 PM
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?

What part are you having the most problems with?

NSG10
Sep 2, 2010, 07:11 PM
Basically, I just need an understanding in general, so I can get a twist on it.

lee1210
Sep 2, 2010, 07:29 PM
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.

-Lee

chown33
Sep 2, 2010, 07:48 PM
It helps a lot to understand how computer memory is organized, and what an address means. Here's a nice explanation, with pictures:
http://masters-of-the-void.com/book5.htm

The whole series is pretty good, too.

firewood
Sep 2, 2010, 08:36 PM
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?

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.

robvas
Sep 3, 2010, 07:51 AM
A good exercise is writing a doubly-linked list.

http://en.wikipedia.org/wiki/Linked_list