当一个对象开头有两个星号时,这意味着什么?
**variable
在声明中,这意味着它是一个指向指针的指针:
int **x; // declare x as a pointer to a pointer to an int
使用时,会参考两次:
int x = 1;
int *y = &x; // declare y as a pointer to x
int **z = &y; // declare z as a pointer to y
**z = 2; // sets the thing pointed to (the thing pointed to by z) to 2
// i.e., sets x to 2
它是指针到指针。
更多详情可以查看:指针到指针
例如,对于动态分配多维数组来说,它可能很好:
喜欢:
#include <stdlib.h>
int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
exit or return
}
for(i = 0; i < nrows; i++)
{
array[i] = malloc(ncolumns * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr, "out of memory\n");
exit or return
}
}
这意味着该变量是一个指向指针的指针。
声明变量时指向指针。
在声明之外使用时双指针取消引用。
指向指针的指针。
您可以使用cdecl来解释C类型。
这里有一个在线界面:http://cdecl.org/。在文本字段中输入“int **x”并检查结果。
**变量是双重解引用。如果变量是地址的地址,则结果表达式将是存储在 *variable 中的地址处的左值。
如果它是声明的一部分,它可能意味着不同的事情:
另一方面,type **variable 意味着指向指针的指针,即可以保存另一个变量地址的变量,该变量也是一个指针,但这次指向“type”类型的变量
这意味着该变量被取消引用两次。假设你有一个指向 char 的指针,如下所示:
char** 变量 = ...;
如果你想访问这个指针指向的值,你必须取消引用它两次:
**变量
它是一个指向指针的指针。如果您想指向
array
或 const char *
(字符串),可以使用它。另外,在 Objective-C 和 Cocoa 中,这通常用来指向 NSError*
。
指向另一个指针的指针
** 是一个指向指针的指针。这些有时用于字符串数组。
这是一个指向指针的指针。
就像 if *x 意味着它将包含某个变量的地址,那么如果我说
m=&x 则 m 表示为
int **m