What's the star for?

Discussion in 'iOS Programming' started by cstromme, Jul 30, 2009.

  1. cstromme macrumors regular

    Feb 26, 2007
    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?
  2. SilentPanda Moderator emeritus


    Oct 8, 2002
    The Bamboo Forest
    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!

  3. robbieduncan Moderator emeritus


    Jul 24, 2002
    I'd recommend taking a step back and learning C first. That way you'd know what the *, & and so one do.
  4. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    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?

  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    What book are you reading?
  6. moral-hazard macrumors regular

    Jul 27, 2009
    Palo Alto, CA
    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
    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.
  7. cstromme thread starter macrumors regular

    Feb 26, 2007
    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.

Share This Page