什么是“int* i”和“int** i”的区别?
“int* i”表示指向整数的指针,“int** i”表示指向指向整数的指针的指针。
“int* i”表示指向整数的指针,“int** i”表示指向指向整数的指针的指针。
文本替换在这里非常有用,但要注意不要盲目使用,因为它可能会误导你(如下面的高级示例所示)。
T var; // var has type T
T* var; // var has type "pointer to T"
无论 T 是什么,这都可以工作:
int* var; // pointer to int
char* var; // pointer to char
double* var; // pointer to double
// advanced (and not pure textual substitution):
typedef int int3[3]; // confusing: int3 has type "array (of size 3) of ints"
// also known as "int[3]"
int3* var; // pointer to "array (of size 3) of ints"
// aka "pointer to int[3]"
int (*var)[3]; // same as above, note how the array type from the typedef
// gets "unwrapped" around the declaration, using parens
// because [] has higher precedence than *
// ("int* var[3];" is an array (size 3) of pointers to int)
当T本身是指针类型时,这个函数可以正常工作:
typedef int* T; // T is a synonym for "pointer to int"
T* var; // pointer to T
// which means pointer to pointer to int
// same as:
int** var;
这不是一个声明。声明语法不允许在整个声明周围加上()
。这些()
在那里做什么?如果这应该是函数声明的一部分,请在您的问题中包含整个函数声明内容,因为在一般情况下,声明的实际含义可能取决于此。(虽然在这种情况下不是这样。)
至于区别...第一个中有一个*
,第二个中有两个*
。这有帮助吗?可能不会。第一个声明i
为指向int
的指针。第二个声明i
为指向int *
的指针。这有帮助吗?可能也不太有用。没有更具体的问题,很难提供更有意义的答案。
请提供更多上下文。或者,如果这实际上是最具体的问题,那么请阅读您喜欢的C或C++书籍关于指针的内容。这样广泛而泛泛的问题不是您在网络上问的东西。