PDA

View Full Version : When would you want to use a pointer of a pointer?




MorphingDragon
Sep 4, 2010, 02:24 AM
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.



gnasher729
Sep 4, 2010, 02:36 AM
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.

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?

larswik
Sep 4, 2010, 02:44 AM
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

subsonix
Sep 4, 2010, 03:11 AM
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.

MorphingDragon
Sep 4, 2010, 04:35 AM
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?

Im rather proud of myself, that didn't fly over my head. :D

lee1210
Sep 4, 2010, 08:43 AM
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