Here is the context:
int *t[10];
int n;
I am being told that tn
is of the type int**. I don't exactly get what int** means, is it a pointer of a pointer? Why would the subtraction of a table of pointers - int would give a pointer of pointer of an 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?
Thanks in advance for explaining this to me.
I don't exactly get what
int**
means, is it a pointer of a pointer?
Yes.
int
= integer int *
= pointer-to-integer int **
= pointer-to-(pointer-to-integer) int ***
= pointer-to-(pointer-to-(pointer-to-integer)) Why would the subtraction of a table of pointers - int would give a pointer of pointer of an int?
Because in C (and C++), an array decays into a pointer to the first item when necessary. For example, int *t[10]
is an array of 10 pointer-to-int items. t
can decay into a pointer to t[0]
, ie a pointer-to-(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).
When we refer to
*t[0]
do we refer toint* 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?
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 *
. Therefore *t[0]
dereferences the first pointer in the array, yielding the actual int
value that the pointer is pointing to.
what
int**
means
It's the type of a pointer to a pointer to an int
. If you dereference a variable t
of this type (like this: *t
), you get a pointer to an int
. If you dereference it twice (like this: **t
), you get an int
.
If you have TYPE a[N];
, the array expression a
, if evaluated, produces a pointer of type T *
, pointing to a[0]
. This is sometimes called C's array-to-pointer "decay" rule.
If TYPE
is int *
, as in your case, then TYPE *
is int **
.
Since your array is of int *
pointers, the pointer which indexes into the array is a necessarily pointer to that element type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.