1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Returning a pointer

Discussion in 'Mac Programming' started by jwei92, Jun 5, 2011.

  1. macrumors newbie

    #1
    I've looked through a couple threads and am still lost on returning pointers.

    I am currently in the process of learning Obj-C with not previous programming background.

    I know some basics, but the only thing I cannot seem to grasp is in Kochan's Objective-C 2.0's book (Chapter 8 - A Point Class and Memory Allocation)

    I know how to allocate, but here are the parts that confuse me:

    Code:
    @interface Rectangle:NSObject 
    {
        int width, height;
        [I]XYPoint *origin;[/I]
    }
    
    [I]-(XYPoint*)origin;
    -(void)setOrigin:(XYPoint*)pt;[/I]
    
    I do not understand why the method returns pointers and such. Sorry if my question isn't clear, but can someone elaborate on why you return a pointer and not just an integer?
     
  2. macrumors 68040

    lee1210

    #2
    All objects are gotten to in Objective-C via a pointer. That's it. Passing an object? You pass a pointer. Getting one back? Pointer. End of story. If it's just an int, sure... just return the int. There may be some reason to return a pointer to a primitive (maybe you want to modify it and have that affect something else that also uses the value in that memory location), but with objects there is no question.

    -Lee
     
  3. macrumors 65816

    #3
    That was the point I got to in that same book. When he started in with pointers and self it got complicated. I then put that book down and Thought, Objective C is built on top of C. So I got the book "Learn C on the Mac" http://www.amazon.com/Learn-C-Mac-Dave-Mark/dp/1430218096 and that book was pretty easy to follow, this was end of last year.

    I have now started back on learning Objective C and from the C book I understand all of the pointer concepts, data structures , functions and so on. Since I liked the Aprees book Learn C on the Mac I decided to start with Learn Objective C for Absolute Beginners by the same company. I just started reading it the other day.

    If it gets hard for you, like me, then I recommend taking it a step back to something simpler. This is from experience.

    -Lars
     
  4. macrumors 6502

    #4
    +1 These are the books I started with. They are much easier to digest than Kochan's book. Kochan's book is a great 3rd book to read.
     

Share This Page