简体   繁体   English

使用 & 声明指向数组的指针

[英]Use of & in declaring pointers to array

To make a pointer to a whole array we proceed like that:为了创建一个指向整个数组的指针,我们这样进行:

    int arr[3] = {1,2,3};
    int (*p)[3] = &arr;

How come i get an incompatibility error when trying to do the same with a 2D array?尝试对 2D 数组执行相同操作时,为什么会出现不兼容错误?

    int arr[3][3] = {{12,10,45}, {44,55,66}, {79,85,91}};
    int (*p)[3] = &arr;

The problem here is &.这里的问题是&。

I'm sure this is a simple question that might have already been answered but i don't find any answer to this specific issue concerning the use of &.我确信这是一个可能已经回答的简单问题,但我没有找到任何关于使用 & 的具体问题的答案。

You've got p in the second example as a pointer to a 1D array, not a pointer to a 2D array.在第二个示例中,您将p作为指向一维数组的指针,而不是指向二维数组的指针。 For that, you need the following:为此,您需要以下内容:

int (*p)[3][3] = &arr;

arr has type int [3][3] , so &arr has type int (*)[3][3] . arr的类型为int [3][3] ,因此&arr的类型为int (*)[3][3] So you can use:所以你可以使用:

int arr[3][3] = {{12,10,45}, {44,55,66}, {79,85,91}};
int (*p)[3][3] = &arr;

You can then access p as (*p)[i][j] .然后,您可以访问p作为(*p)[i][j]

But it would be more typical to use:但它会更典型地使用:

int arr[3][3] = {{12,10,45}, {44,55,66}, {79,85,91}};
int (*p)[3] = arr;

In this case, int [3][3] is compatible with an int (*)[3] pointer, and you can use p[i][j] when accessing it.在这种情况下, int [3][3]int (*)[3]指针兼容,您可以在访问时使用p[i][j]

An analogous situation exists with one-dimensional arrays.一维 arrays 也存在类似的情况。 If you have int x[3] , then the usual pointer type to use is int *p , in which case you would do p = x and access it as p[i] .如果你有int x[3] ,那么通常使用的指针类型是int *p ,在这种情况下你会做p = x并作为p[i]访问它。

But if you really want the extra level of indirection, you could use:但是如果你真的想要额外的间接级别,你可以使用:

int (*p)[3] = &x;

Then instead of p[i] , you would now need to use (*p)[i] when accessing it.然后,您现在需要在访问时使用(*p)[i]而不是p[i]

In C, "pointer" is a category of types.在 C 中,“指针”是类型的一个类别 To get a particular type, you have to specify what type of object the pointer points to .要获得特定类型,您必须指定指针指向的 object 的类型。

Accordingly, the unary & does not generically compute a pointer.因此,一元&一般不计算指针。 It creates a pointer to the type of its operand.它创建一个指向其操作数类型的指针。

Similarly, in C, "array" is a category of types.同样,在 C 中,“数组”是类型的一个类别。 To get a particular type, you have to specify the element type, the number of dimensions , and the sizes of at least the last n-1 dimensions.要获得特定类型,您必须指定元素类型、维度数以及至少最后 n-1 个维度的大小。

Thus, with因此,与

 int arr[3] = {1,2,3}; int (*p)[3] = &arr;
  • arr is defined as an array of 3 int , therefore arr被定义为 3 个int的数组,因此
  • &arr has type pointer to array of 3 int (spelled int (*)[3] as a C type name), and &arr具有指向 3 int数组的类型指针(拼写为int (*)[3]作为 C 类型名称),并且
  • p is declared with that type p是用该类型声明的

so everything is consistent.所以一切都是一致的。

How come i get an incompatibility error when trying to do the same with a 2D array?尝试对 2D 数组执行相同操作时,为什么会出现不兼容错误?

Because your "the same" is not analogous.因为您的“相同”不是类比。

With

 int arr[3][3] = {{12,10,45}, {44,55,66}, {79,85,91}}; int (*p)[3] = &arr;

, arr has type array of 3 array of 3 int , and &arr has type pointer to array of 3 array of three int (aka int (*)[3][3] ). arr具有 3 个int的 3 个数组的类型数组,而&arr具有指向 3 个三个int数组的数组的类型指针(又名int (*)[3][3] )。 This is not the same type that p has .这与p的类型不同 The appropriate declaration of p for this version of arr would be对于此版本的arr ,适当的p声明将是

    int (*p)[3][3] = &arr;

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

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