I happened to stumble upon posts here on SO regarding the double star (**
) usage with NSError
. Decided to dig it a little deeper, which led me revisiting C
.
I have already read many posts here (not a duplicate here) on NSError
and I understand why they do so.
So, I decided to write a little snippet in the IDE (Xcode) to see it myself in action.
int a = 10;
int b = 20;
int *c = &a;
int *d = &b;
int **e = &d;
NSLog(@"Before: a is: %d b is: %d", a, b);
[self testSingleStar:c];
[self testDoubleStar:e];
NSLog(@"After: a is: %d b is: %d", a, b);
Methods:
- (void)testDoubleStar:(int**)x
{
int anotherValue1 = 22;
**x = anotherValue1;
}
- (void)testSingleStar:(int*)x
{
int anotherValue2 = 33;
*x = anotherValue2;
}
Output:
Before: a is: 10 b is: 20
After: a is: 33 b is: 22
Both values of a
(via a single pointer) and b
(via a double pointer) did get changed.
So, if we can change the value (of a
) using only a single pointer, why would we need to use a pointer to a pointer to NSError
so that we could modify the NSError
object in method call where NSError
object is passed in its parameter (&error
)?
In addition:
In regards to the above, passing something to the parameter in C
is actually passing a copy to the parameter (even in the case of a single pointer only a copy of it will get passed), so anything change made within the scope of the method will not effect the original value passed to it (that's why they use a double star).
But the above sample, in which only a single pointer is used, seems to contradicts this because the value (namely a
pointed to by a single pointer - c
) did get changed?
This is because you can't just have an NSError object... you can only deal with Objective-C objects by pointer... so while you could theoretically take a pointer to NSError and swap its contents... it would mess up encapsulation (other things could be relying on that data that used to be there.).
also just reassigning the pointer to a new value doesn't work, because it doesn't affect the callers version.
so while it seems tempting to:
or