C++中的**是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/644981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:27:36  来源:igfitidea点击:

What is ** in C++?

c++pointers

提问by agscala

I've seen some code, as well as some errors generated from my compiler that have a '**' token before the variable (eg **variablename unreferenced-- or something, I can't recall exactly offhand). I'm fairly certain this is related to pointers, if I had to guess it looks like it's trying to dereference twice. '**' is fairly ungoogleable. Can someone point me to a good website/documentation or would someone care to explain it here?

我看过一些代码,以及我的编译器生成的一些错误,这些错误**在变量之前有一个 ' ' 标记(例如 **variablename unreferenced-- 或者其他东西,我无法准确地回忆起来)。我相当肯定这与指针有关,如果我不得不猜测它看起来像是试图取消引用两次。' **' 是相当不可搜索的。有人可以给我指出一个好的网站/文档,还是有人愿意在这里解释一下?

Thanks.

谢谢。

Great responses. If I can add, what would be some situations where it is useful to have a pointer to a pointer? Shouldn't you just be using the original pointer instead of creating yet another pointer to the original pointer?

很棒的反应。如果我可以添加,在哪些情况下使用指向指针的指针是有用的?您不应该只使用原始指针而不是创建另一个指向原始指针的指针吗?

回答by Antti Huima

** is not actually only pointer to pointer (as in declaration), but is also the dereference of a dereference (in a statement).

** 实际上不仅是指向指针的指针(如在声明中),而且还是对解引用的解引用(在语句中)。

It is used often in C which does not have the & notation for references, e.g. to update a return value which is a pointer type:

它经常在 C 中使用,它没有引用的 & 符号,例如更新一个指针类型的返回值:

int alloc_foo(struct foo **foo_ret)
{
    *foo_ret = malloc(sizeof(struct foo));
    return 1; /* to indicate success; return value in foo_ret */
}

回答by Parappa

You may recognize the signature for main():

您可能会认出 main() 的签名:

int main(int argc, char* argv[])

The following is equivalent:

以下是等效的:

int main(int argc, char** argv)

In this case, argv is a pointer to an array of char*.

在这种情况下,argv 是一个指向 char* 数组的指针。

In C, the index operator [] is just another way of performing pointer arithmetic. For example,

在 C 中,索引运算符 [] 只是执行指针运算的另一种方式。例如,

foo[i]

produces the same code as

产生相同的代码

*(foo + i)

回答by Rob Kennedy

It's not a **token. It's simply a *token followed by another *token. In your case, you have a pointer to a pointer, and it's being dereferenced twice to get whatever's really being pointed to.

它不是**令牌。它只是一个*令牌,然后是另一个*令牌。在您的情况下,您有一个指向指针的指针,并且它被取消引用两次以获取真正指向的内容。

回答by Can Berk Güder

**is a pointer to a pointer.

**是一个指向指针的指针。

It might be a matrix (an array of arrays) or an array of strings (a chararray), etc.

它可能是一个矩阵(数组数组)或字符串char数组(数组)等。

回答by Paul Beckingham

It's a double dereference.

这是一个双重取消引用。

int i = 3;
int* ptr_to_i = &i;
int** ptr_to_ptr_to_i = &ptr_to_i;

std::cout << **ptr_to_ptr_to_i << std::endl;

Prints 3.

打印 3。

回答by P Daddy

I just wanted to underscore some of the uses for a pointer to a pointer. Most of these aretouched on by other posts, but I thought reiteration might help.

我只是想强调指向指针的一些用途。其中大部分内容在其他帖子中有所涉及,但我认为重申可能会有所帮助。

  • It allows a callee to modifya pointer owned by the caller. For example, one could pass a pointer to a pointer to the beginning of a string, and the callee could modify the pointed-to pointer to now point to a position within the string where a particular character occurs.

  • Because arrays degrade to pointers (and pointers can be treated as arrays), you will often see a pointer to a pointer if you have:

    • A pointer to an array. This is a generalization of the above case, since a "string" (a C-style string, anyway) is really just an array of chars.

    • An array of pointers. You might, for example, have an array of pointers to objects, allowing for polymorphism, or an array of pointers to select objects stored in another collection.

    • An array of arrays. Again, arrays degrade to pointers, so this is a specific case of the above. This is often used for so called "jagged" arrays (as opposed to rectangular).

  • 它允许被调用者修改调用者拥有的指针。例如,可以传递一个指向字符串开头的指针的指针,被调用者可以修改所指向的指针,使其现在指向字符串中出现特定字符的位置。

  • 因为数组降级为指针(并且指针可以被视为数组),如果您有以下情况,您将经常看到指向指针的指针:

    • 指向数组的指针。这是上述情况的概括,因为“字符串”(无论如何都是 C 风格的字符串)实际上只是一个chars数组。

    • 指针数组。例如,您可能有一个指向对象的指针数组,允许多态性,或者一个指针数组来选择存储在另一个集合中的对象。

    • 数组数组。同样,数组降级为指针,所以这是上述情况的一个特例。这通常用于所谓的“锯齿状”阵列(与矩形相反)。

回答by BC.

You can interpret it literally -- pointer to a pointer

你可以从字面上解释它——指向一个指针的指针

回答by Tony the Pony

  • int **vardeclares a pointer to a pointer
  • **varreferences the content of a pointer, which in itself points to a pointer
  • int **var声明一个指向指针的指针
  • **var引用一个指针的内容,它本身指向一个指针

回答by Martin Beckett

One common use is that it allows a function to set the pointer to null.
So free(pointer) frees up the memory allocated to pointer but leaves the pointer dangerously pointing at the free memory.
Instead declare a my_free(**pointer) and call my_free(&pointer) so my_free() can set the pointer to null after freeing it.

一个常见的用途是它允许函数将指针设置为空。
所以 free(pointer) 释放了分配给指针的内存,但让指针危险地指向了空闲内存。
而是声明一个 my_free(**pointer) 并调用 my_free(&pointer) 以便 my_free() 可以在释放指针后将指针设置为 null。