When would you want to use a pointer of a pointer?

Discussion in 'Mac Programming' started by MorphingDragon, Sep 4, 2010.

  1. macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #1
    When would you want to use a pointer of a pointer? I see it in Cocoa and some other Objective-C & C frameworks and it really doesn't make sense to me.
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Let's say I ask you write a function that calculates two integer values and returns them. How would you write this function?

    Now I ask you to write a function that find three NSString* values and returns them, let's say a function where I give you an NSString* with English text, and you should return three NSString* containing the same text in German, Italian and French. What would the function declaration look like?
     
  3. macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    I am learning pointers in my book and had the same question too. Why can't you just send message to an objects method to return the instance variable? Chown33 showed me this link this morning and it shed light on pointers. gnasher729 response is still way above my skill level I and could not do that.

    http://masters-of-the-void.com/book5.htm

    -Lars
     
  4. macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    Two dimensional arrays is one example, or if you need to manipulate the pointer itself inside a function. In such a case you need a pointer pointing to the pointer, since sending in the original pointer will only give you a local copy, where changes is not seen outside the scope.
     
  5. thread starter macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #5
    Im rather proud of myself, that didn't fly over my head. :D
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    In Cocoa a common usage is passing an NSError **. This allows a brand new NSError object to be returned, as otherwise if an NSError * was passed in it would be unusable as it is immutable.

    Outside of Objective-C there are many times that this comes up, most of which have already been listed. It's probably impossible to make an exhaustive list, because it's all just data and how to get to it.

    -Lee
     

Share This Page