C语言 指向指针数组的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6130712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:46:24  来源:igfitidea点击:

Pointer to Array of Pointers

carrayspointers

提问by moteutsch

I have an array of int pointers int* arr[MAX];and I want to store its address in another variable. How do I define a pointer to an array of pointers? i.e.:

我有一个 int 指针数组,int* arr[MAX];我想将它的地址存储在另一个变量中。如何定义指向指针数组的指针?IE:

int* arr[MAX];
int (what here?) val = &arr;

回答by Kos

The correct answer is:

正确答案是:

int* arr[MAX];
int* (*pArr)[MAX] = &arr;

Or just:

要不就:

        int* arr  [MAX];
typedef int* arr_t[MAX];

arr_t* pArr = &arr;

The last part reads as "pArr is a pointer to array of MAX elements of type pointer to int".

最后一部分读作“pArr 是指向 int 类型指针的 MAX 元素数组的指针”。

In C the size of array is stored in the type, not in the value. If you want this pointer to correctly handle pointer arithmetic on the arrays(in case you'd want to make a 2-D array out of those and use this pointer to iterate over it), you - often unfortunately - need to have the array size embedded in the pointer type.

在 C 中,数组的大小存储在类型中,而不是值中。如果您希望此指针正确处理数组上的指针算术(以防您想从中创建一个二维数组并使用此指针对其进行迭代),您 - 通常很不幸 - 需要拥有该数组嵌入在指针类型中的大小。

Luckily, since C99 and VLAs (maybe even earlier than C99?) MAX can be specified in run-time, not compile time.

幸运的是,由于 C99 和 VLA(甚至可能早于 C99?)MAX 可以在运行时指定,而不是在编译时指定。

回答by Jason

Should just be:

应该只是:

int* array[SIZE];
int** val = array;  

There's no need to use an address-of operator on arraysince arrays decay into implicit pointers on the right-hand side of the assignment operator.

array由于数组衰减为赋值运算符右侧的隐式指针,因此无需使用地址运算符。

回答by Etienne de Martel

IIRC, arrays are implicitly convertible to pointers, so it would be:

IIRC,数组可以隐式转换为指针,所以它将是:

int ** val = arr;

回答by VSarin

According to this source http://unixwiz.net/techtips/reading-cdecl.html, by using the "go right when you can, go left when you must" rule, we get the following 2 meanings of the declarations given in the previous answers -

根据此来源http://unixwiz.net/techtips/reading-cdecl.html,通过使用“可以时向右走,必须时向左”规则,我们得到了以下 2 种含义以前的答案 -

int **val ==> val is a pointer to pointer to int
int* (*pArr)[MAX] ==> pArr is a pointer to an array of MAX length pointers to int.

I hope the above meanings make sense and if they don't, it would probably be a good idea to peruse the above mentioned source.

我希望上述含义有意义,如果没有,仔细阅读上述来源可能是个好主意。

Now it should be clear that the second declaration is the one which moteutsch is looking for as it declares a pointer to an array of pointers.

现在应该清楚第二个声明是 moteutsch 正在寻找的声明,因为它声明了一个指向指针数组的指针。

So why does the first one also work? Remember that

那么为什么第一个也有效呢?请记住

int* arr[MAX]

is an array of integer pointers. So, val is a pointer to, the pointer to the first int declared inside the int pointer array.

是一个整数指针数组。因此, val 是一个指针,指向在 int 指针数组中声明的第一个 int 的指针。

回答by Ruben Gon?alves

I believe the answer is simply:

我相信答案很简单:

int **val;
val = arr;

回答by Tom Surya Sylvester Shah

#define SIZE 10
int *(*yy)[SIZE];//yy is a pointer to an array of SIZE number of int pointers
and so initialize yy to array as below -

int *y[SIZE];   //y is array of SIZE number of int pointers
yy = y;         // Initialize
//or yy = &y;   //Initialize

回答by dhblah

As far as I know there is no specific type "array of integers" in c, thus it's impossible to have a specific pointer to it. The only thing you can do is to use a pointer to the int: int*, but you should take into account a size of int and your array length.

据我所知,中没有特定类型的“整数数组” c,因此不可能有指向它的特定指针。您唯一可以做的就是使用指向 int: 的指针int*,但您应该考虑 int 的大小和数组长度。