简体   繁体   English

将指针传递给多维数组

[英]pass pointer to multidimensional array

I have a two dimensional array of ints which represents a few dots in a graph. 我有一个二维的int数组,代表图中的几个点。

int16_t myPoints[][3] = {
  {0, 0},
  {20, 40},
  {14, 92}};

How can I pass a pointer to a function which will be able to read these coordinates? 如何将指针传递给能够读取这些坐标的函数?

something like? 就像是?

void foo(int16_t *coordinates[][]) {
   drawPoint(x[0], y[0]);
   drawPoint(x[1], y[1]);
   ...

Thanks for the help. 谢谢您的帮助。

Only the first dimension can be unknown when declaring a function, so you have to declare/define the function like this: 在声明函数时,只有第一个维度是未知的,因此您必须声明/定义函数,如下所示:

void foo(int16_t coordinates[][3])
{
    drawPoint(coordinates[0][0], coordinates[1][0]);
    drawPoint(coordinates[0][1], coordinates[1][1]);
    drawPoint(coordinates[0][2], coordinates[1][2]);
}

Then you can call it like a normal function: 然后你可以像普通函数一样调用它:

foo(myPoints);

Edit: Your array declaration is not correct, and I missed it too. 编辑:您的数组声明不正确,我也错过了。 It should be: 它应该是:

int16_t myPoints[][2] = {
  /* List of points */
};

Now you have a proper coordinate-array, and it can be as long as you like. 现在你有一个合适的坐标数组,只要你喜欢它就可以了。

For the function to know the number of entries, you have to pass it to the function, so the new function declaration will be: 对于知道条目数的函数,您必须将它传递给函数,因此新的函数声明将是:

void foo(int16_t coordinates[][2], int number_of_points);

Don't worry about copying, the compiler is smart enough to only pass the pointer to the array, so it will not copy the whole array. 不要担心复制,编译器足够智能只能将指针传递给数组,因此它不会复制整个数组。

The rule is that when an expression of type "N-element array of T " appears in most contexts, it will be converted to an expression of type "pointer to T ". 规则是当在大多数上下文中出现类型为“N元素数组T ”的表达式时,它将被转换为“指向T指针”类型的表达式。

myPoints is an expression of type "3-element array of 3-element arrays of int16_t ". myPoints是类型“的3元素数组的3元素数组的表达int16_t ”。 Thus, when myPoints appears in most contexts 1 (such as a function call), it will be replaced with an expression of type "pointer to 3-element array of int16_t ", or int16_t (*)[3] . 因此,当myPoints出现在大多数上下文1 (例如函数调用)中时,它将被替换为“指向int16_t 3元素数组的int16_t ”或int16_t (*)[3]

If you want to call foo as foo(myPoints) , then the prototype will need to be 如果你想把foo称为foo(myPoints) ,那么原型就需要

void foo(int16_t (*coordinates)[3])

or 要么

void foo(int16_t coordinates[][3])

In the context of a function parameter declaration, T a[N] , T a[] , and T *a are all equivalent, and all declare a as a pointer to T . 在函数参数声明的上下文中, T a[N]T a[]T *a都是等价的,并且都a a声明为指向T的指针。 Note that this is only true in a function parameter declaration. 请注意,这适用于函数参数声明。


1 - The exceptions to the rule are when the array expression is an operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration. 1 - 规则的例外是当数组表达式是sizeof或一元&运算符的操作数时,或者是用于初始化声明中的另一个数组的字符串文字。

By repeating the proper declaration for the function's argument: 通过重复函数参数的正确声明:

void foo(int16_t coordinates[][3])
{
  ...
}

You must include all array dimensions except the one immediately following the variable name, which is optional. 您必须包括紧跟变量名后面的所有数组维,这是可选的。

but does that copy the whole array to foo or does it just pass the pointer to it? 但是这会将整个数组复制到foo还是只是将指针传递给它? I also just know that there will ALWAYS be two coordinates per entry, but I don't know how many entries there will come. 我也知道每个条目总共会有两个坐标,但我不知道会有多少条目。

If you're passing a variable length array, you either have to pass the number of elements as a separate parameter or the last element of the array must somehow indicate that it is the last element so that you can terminate the function using a while() loop (like adding a NULL to terminate a string). 如果要传递一个可变长度数组,则必须将元素数作为单独的参数传递,否则数组的最后一个元素必须以某种方式指示它是最后一个元素,以便您可以使用while终止函数( )循环(如添加NULL以终止字符串)。

enum STATUS { OK, END };
STATUS bar(int16_t x, int16_t y);  /* bar() checks to see if this is the last element */

void foo(int16_t coordinates[][2])
{
    int i = 0;
    while( END != bar(coordinates[i][0], coordinates[i][1]) )
    {
        drawPoint(coordinates[i][0], coordinates[i][1]);
        ++i;
    }
}

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

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