Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cstromme

macrumors regular
Original poster
Feb 26, 2007
162
0
I'm just starting to learn objective-c and Cocoa here, and I'm a bit puzzled by the use of a * in front of variables. My book doesn't say anything about it, and the Stanford class doesn't either. Tried searching but that didn't give me any clear answers either.

So what is the * in front of variables good for, and when do you need it? Also saw a piece of code where someone used ** in front of a variable, was that a typo?
 
I believe the * is to denote that it is a pointer. In short a pointer stores the memory location where the variables data exists and not the actual variable data.

I didn't know you could do this but apparently you can use **, ***, ****, etc for multiple indirection... it makes sense to me but seems like something you would want to avoid if you could for sanity's sake though!

http://boredzo.org/pointers/
 
If you don't learn enough about computer programming in general, and C in particular, then there is a good probability you will screw up when dealing with handling pointers, memory address, and the type and contents of memory locations (which is pretty much everything).

Start there and continue learning until you can explain to someone else why the star is there; and even more importantly, why it is not there in most cases, and if it's not there, where is your data?


imho.
 
That is in front of variables considered to be "objects", and does not apply to primitives (unless you want pointers to them...)

You handle objects in Obj-C using pointers to some address in memory where the object resides.

So if you say
Code:
UIView *view = [UIView alloc];

You are basically setting a pointer (called view) to point to a block of memory allocated with enough space for a UIView object.
 
Thanks for the explanation guys. :)

So why do I not have to do this certain times?

I'm just starting on obj-c and Cocoa here, coming from a Java background. I'm not planning on starting a pure C book, but thanks for the recommendation. I should be able to learn these things while learning obj-c.

Perhaps someone could point me to some reading (shorter than a book) that I could check out?

EDIT: Oh, nevermind, didn't see the link in the first reply right away.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.