Theocacao
Leopard
Design Element
Comment on "Intro to C Pointers and Dynamic Memory"
by Scott Stevenson — Jan 02
Why and when should you use pointers?

There are many things you simply cannot do without them. In a sense, pointers are a way for different parts of a program to access the same data. In some cases, they're the only way. In fact, the only thing you can really do without pointers is pass around individual number values or single characters. As soon as you want to use arrays, strings (blocks of text), or Cocoa objects you need pointers.

I think a lot of the confusion from pointers stems from the name. It make it sounds like a pointer is active, that it does something. It really does not. It's just a number that uniquely indentifies a piece of data in memory. It might be easier to actually think of them as unique IDs.

In Cocoa, all object variables are pointers because all of them are stored in the same central location -- in "heap" memory. That is, if you create an object inside a function, it doesn't automatically "go away" at the end of the function. The only way to "reach" the object in heap memory is to refer to it by its unique ID -- the pointer value.

I hope that helps.
Back to "Intro to C Pointers and Dynamic Memory"
Design Element

Copyright © Scott Stevenson 2004-2015