Monday, April 11, 2011

What is double star?

So, I saw this:

error:(NSError **)error

in the apple doc's. Why two stars? What is the significance?

From stackoverflow
  • If it is anything like C then ** means a pointer to a pointer.

    Peter Hosey : Objective-C is a strict superset of C. Nothing from C is any different.
  • In C, a double star is a pointer to a pointer. There are a couple of reasons to do this. First is that the pointer might be to an array of pointers. Another reason would be to pass a pointer to a function, where the function modifies the pointer (similar to an "out" parameter in other languages).

  • A "double star" is a pointer to a pointer. So NSError ** is a pointer to a pointer to an object of type NSError. It basically allows you to return an error object from the function. You can create a pointer to an NSError object in your function (call it *myError), and then do something like this:

    *error = myError;
    

    to "return" that error to the caller.

  • In C everything is pass by value. If you want to change the value of something you pass the address of it (which passes the value of the memory address). If you want to change where a pointer points you pass the the addres of the pointer.

    Take a look here for a simple explanation.

  • It is a song by the Rolling Stones

    http://en.wikipedia.org/wiki/Star_Star

    Georg : Hard to decide whether to vote it up or down. :)
    Otávio Décio : Monday afternoon is tough... :)

0 comments:

Post a Comment