Results 1 to 3 of 3
  1. #1

    Member Since
    Dec 30, 2009
    Question Pointers in Objective-C
    I'm just now teaching myself how to code in Objective-C (however, I have no background in C or any other extension), and I'm having some trouble understanding pointers. While reading around, I noticed a lot of people explaining that all objects are just pointers referencing objects. If this is true, then why do I ever have to explicitly create pointers? What is the difference between the follow two statements?

    int numberOne = 12;
    int *numberTwo = 14;

    From what I remember from Java, it would follow that I could do numberOne = numberTwo, and therefore both are equal to 14. I can, however, still change either number individually. i.e. numberTwo = 17 (and therefor numberOne = 14 and numberTwo = 17). Of course, if I were to do the opposite, then the pointer numberTwo would point to the same object as numberOne, and every time I changed one of them, "both" would change.

    But if all variables are actually just pointers, then aren't these two phrases exactly the same?

    numberOne = numberTwo;
    numberTwo = numberOne;

    I have about five ebooks on Cocoa or Objective-C, and I'm only three chapters into my first book, so if any of you can reference a book or website for me to check out, or just shed some light on my problem with understanding, that would be faaantastic.

  2. #2

    Raz0rEdge's Avatar
    Member Since
    Jul 17, 2009
    27" i7 iMac, 15" Macbook Pro TB, 13" Macbook Air, iPhone 6S, iPod Nano 7th Gen
    In the realm of C, you would use pointers to pass it's reference to a function as opposed to the value itself..

    Your example above, (although buggy) is how you'd do it in C. The problem is that numberTwo has no "storage" that it's pointing to, so when you assign 14 to it, that becomes the "storage" and later one if you try to de-reference the pointer, the program will crash.

    Within Objective-C, you still use pointers and you have to to be able to send messages to the class object..

    For example:
    Fraction *myFraction = [[Fraction alloc] init];
    [myFraction somemethod];
    The [] syntax in Obj-C is doing a bunch of work the first line, it will call your Fraction class' alloc function, followed by init function. What is returned is a instance of the object fully initialized. You can now call other methods within that class with the second line and pass in any arguments and so on.

    Pointers are very useful much so that certain languages make EVERYTHING a pointer (visibly or not)..but there're also a lot of easy pitfalls to get into..


  3. #3

    Member Since
    Dec 09, 2008
    I don't know objective-c, i aim to learn though. In C however a good example of the use of pointers is a function that swaps two values. eg

    voide main()
        int val1, val2;
        swap (val1, val2);
    int swap (int val1, int val2)
         int temp = val1;
         val2 = val1;
         val1 = temp;

    This will not work. The values are copied into the swap() function and swapped within the scope of the function, but not externaly to it. However, if we pass in pointers to the original variables we can indirectly access them and the swap will occure on the original values.

    I hope this helps!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I need a few pointers with preview
    By Yetitrician in forum macOS - Apps and Games
    Replies: 3
    Last Post: 09-24-2012, 07:39 PM
  2. Getting my first mac but need a few pointers
    By aasterix in forum Apple Notebooks
    Replies: 7
    Last Post: 12-28-2009, 11:29 AM
  3. need some pointers for using Panther
    By bargsbeer in forum Switcher Hangout
    Replies: 4
    Last Post: 12-02-2007, 02:36 AM
  4. Pointers on how to go wireless
    By Kuss in forum Internet, Networking, and Wireless
    Replies: 0
    Last Post: 09-24-2004, 02:17 AM
  5. pointers
    By nwilcox in forum macOS - Operating System
    Replies: 1
    Last Post: 08-09-2004, 10:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts