什么是双星(例如 NSError **)?

发布于 2024-07-14 13:56:14 字数 103 浏览 7 评论 0原文

所以,我看到了这个:

error:(NSError **)error

在苹果文档中。 为什么是两星? 有什么意义呢?

So, I saw this:

error:(NSError **)error

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寒江雪… 2024-07-21 13:56:14

“双星”是指向指针的指针。 所以 NSError ** 是一个指向 NSError 类型对象的指针。 它基本上允许您从函数返回错误对象。 您可以在函数中创建一个指向 NSError 对象的指针(将其称为 *myError),然后执行如下操作:

*error = myError;

将该错误“返回”给调用者。


回复下面发布的评论:

您不能简单地使用 NSError * 因为在 C 中,函数参数是按值传递的 - 也就是说,值是 >传递给函数时复制。 为了说明这一点,请考虑以下 C 代码片段:

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

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

f() 中对 x 的重新分配不会影响 f() 之外的参数值(例如,在 g() 中)。

同样,当将指针传递给函数时,它的值会被复制,并且重新分配不会影响函数外部的值。

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!
}

当然,我们知道我们可以相当容易地更改 z 所指向的值:

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"
}

因此,按理说,要更改 NSError * 所指向的值为了,我们还必须传递一个指向该指针的指针。

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\n", 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\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 what z points to fairly 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 stands to reason that, to change the value of what an NSError * points to, we also have to pass a pointer to the pointer.

等风来 2024-07-21 13:56:14

在 C 中,一切都是按值传递的。 如果你想改变某个东西的值,你可以传递它的地址(传递内存地址的值)。 如果你想改变指针指向的位置,你可以传递指针的地址。

查看此处的简单说明

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.

海螺姑娘 2024-07-21 13:56:14

在 C 语言中,双星是指向指针的指针。 这样做有几个原因。 首先,该指针可能指向一个指针数组。 另一个原因是将指针传递给函数,其中函数修改指针(类似于其他语言中的“out”参数)。

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).

初心 2024-07-21 13:56:14

双星 (**) 表示法并非特定于初始化类中的变量。 它只是对对象的双重间接引用。

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

双指针表示法用于调用者希望通过函数调用修改其自己的指针之一的情况,因此将指针的地址而不是对象的地址传递给函数。

一个例子可能是链表的使用。 调用者维护一个指向第一个节点的指针。 调用者调用函数来搜索、添加和删除。 如果这些操作涉及添加或删除第一个节点,则必须更改调用者的指针,而不是任何节点中的 .next 指针,并且您需要指针的地址来执行此操作。

The double star (**) notation is not specific to initializing a variable in a class. It is simply a double indirect reference to an 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

Double pointer notation is used where the caller intends that one of its own pointers need to be modified by a function call, so the address of the pointer, instead of the address of the object, is passed to the function.

An example might be the use of a linked list. The caller maintains a pointer to the first node. The caller invokes functions to search, add, and remove. If those operations involve adding or deleting the first node, then the caller's pointer has to change, not the .next pointer in any of the nodes, and you need the address of the pointer to do that.

烂人 2024-07-21 13:56:14

如果它是类似 C 的东西,那么 ** 表示指向指针的指针。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文