Arrays 用于处理不同大小的多维数组的函数

Arrays 用于处理不同大小的多维数组的函数,arrays,pointers,visual-c++,multidimensional-array,arduino,Arrays,Pointers,Visual C++,Multidimensional Array,Arduino,长话短说,我正在尝试为各种事情处理一些数组。在调试期间,我需要打印阵列。我希望只有一个函数可以打印传递给它的任何数组。我也可以相当容易地传递数组的维度。现在让我们假设,我只使用二维数组。 相信我;我已经审查了这里提出的每一个类似问题,并试图实施建议的解决方案,但没有任何效果。我试过使用指针之类的东西。特别是,我在这篇文章中尝试了所有四种解决方案: ,根据方法的不同,我仍然会遇到无数错误。 我不是新手,但我对C++是新的。 下面是一个非常简单的示例,说明我正在尝试做的事情: // Array F

长话短说,我正在尝试为各种事情处理一些数组。在调试期间,我需要打印阵列。我希望只有一个函数可以打印传递给它的任何数组。我也可以相当容易地传递数组的维度。现在让我们假设,我只使用二维数组。
相信我;我已经审查了这里提出的每一个类似问题,并试图实施建议的解决方案,但没有任何效果。我试过使用指针之类的东西。特别是,我在这篇文章中尝试了所有四种解决方案: ,根据方法的不同,我仍然会遇到无数错误。 我不是新手,但我对C++是新的。 下面是一个非常简单的示例,说明我正在尝试做的事情:

//  Array Failures
#define FIRSTWIDTH 6        //Number of Columns in First Array
#define FIRSTHEIGHT 5       //Number of Rows in First Array
#define SECONDWIDTH 4       //Number of Columns in Second Array
#define SECONDHEIGHT 2      //Number of Rows in Second Array


//  An couple of arrays are declared with dimensions WIDTH X HEIGHT
int firstArray[FIRSTWIDTH][FIRSTHEIGHT];
int secondArray[SECONDWIDTH][SECONDHEIGHT];

void setup()
{
Serial.begin(57600);
fillXXArray(FIRSTWIDTH, FIRSTHEIGHT, firstArray);
fillXXArray(SECONDWIDTH, SECONDHEIGHT, secondArray);  //GENERATES ERROR 1
Serial.println("Program Initialized");
printANYArray(FIRSTWIDTH, FIRSTHEIGHT, firstArray); 
}

void loop(){return;}

void fillXXArray(int width, int height, int myArray[][FIRSTHEIGHT]){
// Fills an array with values that is WIDTH X HEIGHT
// Inner Subset is the sum of its coordinates
for (int w = 0; w < width - 1; w++){
    for (int h = 0; h < height - 1; h++){
        myArray[w][h] = w + h;
    }
}

// Loop across the bottom row and set them to the sum of the column.
for (int w = 0; w < width - 1; w++){
    for (int h = 0; h < height - 1; h++){
        myArray[w][height - 1] = myArray[w][height - 1] + myArray[w][h];
    }
 }

// Loop down the last column and set it to the product of the values in row.
for (int h = 0; h < height - 1; h++){
    myArray[width - 1][h] = myArray[0][h];
    for (int w = 1; w < width - 1; w++){
        myArray[width-1][h] = myArray[width-1][h] * myArray[w][h];
    }
}
myArray[width - 1][height - 1] = width * height;

//  The final array is populated like this.
//
//            0     1     2    ...         w
//    0    [  0     1     2    ...   (Product of Row)  ]
//    1    |  1     2     3    ...   (Product of Row)  |
//    2    |  2     3     4    ...   (Product of Row)  |
//   ...   | ...   ...   ...   ...   (Product or ...)  |
//    h    [(Sum) (Sum) (Sum)  ...       (h*W)         ]
//
}

void printANYArray(size_t width, size_t height, int myArray[][height]){  //GENERATES ERROR 2
Serial.println("printXXArray: ");
for (int h = 0; h < height; h++){
    for (int w = 0; w < width; w++){
        Serial.print(myArray[w][h]); Serial.print("\t\t");
    } Serial.println();
} Serial.println();
}
但这不会在函数中引发标志,但函数调用失败,原因如下:

 printANYArray(FIRSTWIDTH, FIRSTHEIGHT, firstArray);
“firstArray”错误:“int(8)[5]”类型的参数与参数类型“int**”不兼容

下一个解决方案:“函数正在使用指针,所以我不应该给它发送地址吗?”我将函数调用更改为:

printANYArray(FIRSTWIDTH, FIRSTHEIGHT, &firstArray);
仅突出显示“&”,错误:类型为“int(*)[6][5]”的参数与参数类型“int**”不兼容 等待什么?!?我以为我只是把地址寄出去了

下一个猜测(因为我现在就是这么做的): 我删除了声明中的一个*。这只是从错误消息中删除了一个星号。至少这是可以理解的


所以。。。你能提供的任何帮助都会很有帮助。同样,这是针对Arduino的,可能与此有关,因为我似乎无法从示例中获得任何解决方案,即使我复制并粘贴整个源代码

我会避免使用模板,因为在Arduino上,您希望保留 编译代码小。我将转而使用旧式1D阵列:

int firstArray[FIRSTHEIGHT * FIRSTWIDTH];
int secondArray[SECONDHEIGHT * SECONDWIDTH];
在C语言家族中,通常的惯例是存储矩阵 按行,并将第一个索引作为行索引。按此 按照惯例,对于a,第i行第j列的单元格将是
arr[i][j]
“真实”2D数组,以及这些“伪造”2D数组的
arr[width*i+j]
。 以下是您如何填写它们:

void fillXXArray(int *myArray, int height, int width) {

    // Inner Subset is the sum of its coordinates
    for (int h = 0; h < height - 1; h++) {
        for (int w = 0; w < width - 1; w++) {
            myArray[width * h + w] = h + w;
        }
    }

    // Bottom row has the sums of the columns.
    for (int w = 0; w < width - 1; w++) {
        myArray[width * (height - 1) + w] = 0;
        for (int h = 0; h < height - 1; h++) {
            myArray[width * (height - 1) + w] += myArray[width * h + w];
        }
     }

    // Last column has the product of the rows.
    for (int h = 0; h < height - 1; h++) {
        myArray[width * h + (width - 1)] = myArray[width * h + 0];
        for (int w = 1; w < width - 1; w++) {
            myArray[width * h + (width - 1)] *= myArray[width * h + w];
        }
    }

    // Last cell.
    myArray[width * (height - 1) + (width - 1)] = width * height;
}
这里,只有
firstArray[]
的第一个指针被正确初始化。 您必须在运行时初始化rest,例如在
fillXXArray()

void fillXXArray(int**myArray,int高度,int宽度){
//初始化指向行开头的指针。
对于(int h=1;hmyArray[h][w]=h+w;//
模板无效填充xxArray(int(&myArray)[M][N]);
非常感谢Edgar。这不是我所希望的,但肯定会起作用!感谢您的全面回复!
void fillXXArray(int *myArray, int height, int width) {

    // Inner Subset is the sum of its coordinates
    for (int h = 0; h < height - 1; h++) {
        for (int w = 0; w < width - 1; w++) {
            myArray[width * h + w] = h + w;
        }
    }

    // Bottom row has the sums of the columns.
    for (int w = 0; w < width - 1; w++) {
        myArray[width * (height - 1) + w] = 0;
        for (int h = 0; h < height - 1; h++) {
            myArray[width * (height - 1) + w] += myArray[width * h + w];
        }
     }

    // Last column has the product of the rows.
    for (int h = 0; h < height - 1; h++) {
        myArray[width * h + (width - 1)] = myArray[width * h + 0];
        for (int w = 1; w < width - 1; w++) {
            myArray[width * h + (width - 1)] *= myArray[width * h + w];
        }
    }

    // Last cell.
    myArray[width * (height - 1) + (width - 1)] = width * height;
}
int firstArrayData[FIRSTHEIGHT * FIRSTWIDTH];       // actual data
int *firstArray[FIRSTHEIGHT] = { firstArrayData };  // pointers to rows
void fillXXArray(int **myArray, int height, int width) {

    // Initialize the pointers to the start of the rows.
    for (int h = 1; h < height; h++) {
        myArray[h] = myArray[0] + width * h;
    }

    // Inner Subset is the sum of its coordinates
    for (int h = 0; h < height - 1; h++) {
        for (int w = 0; w < width - 1; w++) {
            myArray[h][w] = h + w;      // <-- syntax like 2D arrays
        }
    }

    // etc...