What is a double star (e.g. NSError **)?

So, I saw this:

error:(NSError **)error 

in the apple document. Why two stars? What's the point?

+51
c pointers objective-c cocoa multiple-indirection
Mar 02 '09 at 21:37
source share
5 answers

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

 *error = myError; 

to "return" this error to the caller.




In response to the comment below:

You cannot just use NSError * , because in C the parameters of the function are passed by value, i.e. the values ​​are copied when the function is passed. To illustrate, consider this C code snippet:

 void f(int x) { x = 4; } void g(void) { int y = 10; f(y); printf("%d\n", y); // Will output "10" } 

Reassigning x to f() does not affect the value of the argument outside f() (for example, to g() ).

Similarly, when a pointer is passed to a function, its value is copied, and reassignment does not affect the value outside the function.

 void f(int *x) { x = 10; } void g(void) { int y = 10; int *z = &y; printf("%p\n", z); // Will print the value of z, which is the address of y f(z); printf("%p\n", z); // The value of z has not changed! } 

Of course, we know that we can change the value of the fact that z indicates quite easily:

 void f(int *x) { *x = 20; } void g(void) { int y = 10; int *z = &y; printf("%d\n", y); // Will print "10" f(z); printf("%d\n", y); // Will print "20" } 

So it’s reasonable that to change the value of what NSError * points to, we also need to pass a pointer to a pointer.

+68
Mar 02 '09 at 21:42
source share

In C, everything goes by value. If you want to change the value of something, you pass its address (which passes the value of the memory address). If you want to change where the pointer points, you pass the pointer pointers.

Take a look here for a simple explanation .

+43
Mar 02 '09 at 21:44
source share

In C, a double star is a pointer to a pointer. There are several reasons for this. First, a pointer can be an array of pointers. Another reason would be to pass a pointer to a function where the function changes the pointer (similar to the "out" parameter in other languages).

+10
Mar 02 '09 at 21:40
source share

A double star designation (**) is not specific to initializing a variable in a class. This is just a double indirect reference to the object.

  float myFloat; // an object float *myFloatPtr; // a pointer to an object float **myFloatPtrPtr; // a pointer to a pointer to an object myFloat = 123.456; // initialize an object myFloatPtr = &myFloat; // initialize a pointer to an object myFloatPtrPtr = myFloatPtr; // initialize a pointer to a pointer to an object myFloat; // refer to an object *myFloatPtr; // refer to an object through a pointer **myFloatPtrPtr; // refer to an object through a pointer to a pointer *myFloatPtrPtr; // refer to the value of the pointer to the object 

Binary pointer notation is used where the calling object assumes that one of its own pointers must be changed by a function call, so the address of the pointer, and not the address of the object, is passed to the function.

An example would be to use a linked list. The caller maintains a pointer to the first node. The caller calls functions to search, add, and delete. If these operations include adding or removing the first node, then the caller’s pointer should change, not the .next pointer in any of the nodes, and for this you need the address of the pointer.

+6
Apr 24 '13 at 4:58
source share

If it is something like C, then ** means a pointer to a pointer.

+4
Mar 02 '09 at 21:40
source share



All Articles