Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

c - What is double star (eg. NSError **)?

So, I saw this:

error:(NSError **)error

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 reply to a comment posted below:

You can't simply use an NSError * because in C, function parameters are passed by value—that is, the values are copied when passed to a function. To illustrate, consider this snippet of C code:

void f(int x)
{
    x = 4;
}

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

The reassignment of x in f() does not affect the argument's value outside of f() (in g(), for example).

Likewise, when a pointer is passed into a function, its value is copied, and re-assigning will not affect the value outside of the function.

void f(int *x)
{
    x = 10;
}

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

Of course, we know that we can change the value of what z points to fairly easily:

void f(int *x)
{
    *x = 20;
}

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

So it stands to reason that, to change the value of what an NSError * points to, we also have to pass a pointer to the pointer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...