简体   繁体   English

在这种情况下,C 中的 int** 是什么意思?

[英]What does int** mean in C in this context?

Here is the context:这是上下文:

int *t[10];
int n;

I am being told that tn is of the type int**.我被告知tn是 int** 类型。 I don't exactly get what int** means, is it a pointer of a pointer?我不完全明白 int** 是什么意思,它是指针的指针吗? Why would the subtraction of a table of pointers - int would give a pointer of pointer of an int?为什么指针表的减法 - int 会给出一个 int 指针的指针? When we refer to *t[0] do we refer to int* p to the pointer itself because it is an element of the table or do we implicitly need a pointer to point at the slot than have the pointer point to the another place?当我们引用*t[0]时,我们是否将int* p引用到指针本身,因为它是表的一个元素,还是我们隐含地需要一个指向插槽的指针而不是指针指向另一个位置?

Thanks in advance for explaining this to me.提前感谢您向我解释这一点。

I don't exactly get what int** means, is it a pointer of a pointer?我不完全明白int**是什么意思,它是指针的指针吗?

Yes.是的。

  • int = integer int = integer
  • int * = pointer-to-integer int * = 指向整数的指针
  • int ** = pointer-to-(pointer-to-integer) int ** = 指向指针(指向整数的指针)
  • int *** = pointer-to-(pointer-to-(pointer-to-integer)) int *** = 指针指向(指针指向(指针指向整数))
  • (and so on) (等等)

Why would the subtraction of a table of pointers - int would give a pointer of pointer of an int?为什么指针表的减法 - int 会给出一个 int 指针的指针?

Because in C (and C++), an array decays into a pointer to the first item when necessary.因为在 C(和 C++)中,数组会在必要时衰减为指向第一项的指针。 For example, int *t[10] is an array of 10 pointer-to-int items.例如, int *t[10]是一个包含 10 个指向 int 项的指针的数组。 t can decay into a pointer to t[0] , ie a pointer-to-(pointer-to-int), int ** . t可以衰减为指向t[0]的指针,即指向 (pointer-to-int), int **的指针。 That pointer can then be used for pointer arithmetic (like subtraction).然后可以将该指针用于指针算术(如减法)。

So, subtracting n from t would give you an int ** that is pointing n items "before" the beginning of your 10-item array (which BTW would not be a safe pointer to use, unless n was zero or a small negative number, since it would be pointing outside the valid bounds of the array's memory).因此,从t中减去n会给你一个int ** ,它指向你的 10 项数组的开头“之前”的n项(顺便说一句,这不是一个安全的指针,除非n为零或一个小的负数,因为它将指向数组内存的有效边界之外)。

When we refer to *t[0] do we refer to int* p to the pointer itself because it is an element of the table or do we implicitly need a pointer to point at the slot than have the pointer point to the another place?当我们引用*t[0]时,我们是否将int* p引用到指针本身,因为它是表的一个元素,还是我们隐含地需要一个指向插槽的指针而不是指针指向另一个位置?

I'm not sure I understand this question.我不确定我是否理解这个问题。 Since t[10] is an array of 10 pointers (ie 10 int * 's), that means that t[0] is a single item in that array and therefore has type int * .由于t[10]是一个由 10 个指针组成的数组(即 10 个int * ),这意味着t[0]是该数组中的一个项目,因此具有int *类型。 Therefore *t[0] dereferences the first pointer in the array, yielding the actual int value that the pointer is pointing to.因此*t[0]取消引用数组中的第一个指针,产生指针指向的实际int值。

what int** means int**是什么意思

It's the type of a pointer to a pointer to an int .它是指向int的指针的类型。 If you dereference a variable t of this type (like this: *t ), you get a pointer to an int .如果您取消引用此类型的变量t (例如: *t ),您将获得一个指向int的指针。 If you dereference it twice (like this: **t ), you get an int .如果你取消引用它两次(像这样: **t ),你会得到一个int

If you have TYPE a[N];如果你有TYPE a[N]; , the array expression a , if evaluated, produces a pointer of type T * , pointing to a[0] . ,数组表达式a ,如果求值,产生一个类型为T *的指针,指向a[0] This is sometimes called C's array-to-pointer "decay" rule.这有时被称为 C 的数组到指针“衰减”规则。

If TYPE is int * , as in your case, then TYPE * is int ** .如果TYPEint * ,就像你的情况一样,那么TYPE *int **

Since your array is of int * pointers, the pointer which indexes into the array is a necessarily pointer to that element type.由于您的数组是int *指针,因此索引到数组的指针必然是指向该元素类型的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM