用c ++将矩阵/ 2d数组复制到另一个数组

时间:2021-12-11 21:30:10

I am trying to copy a 2d array into another 2d array, i have these two array created:

我试图将2d数组复制到另一个2d数组中,我创建了这两个数组:

#define mapXcor 50
#define mapYcor 50

char secondStage    [mapXcor][mapYcor];
char currentStage   [mapXcor][mapYcor];
//mapXcor and mapYcor are constant integers

Now my secondStage[][] array is populated with values on it, but currentStage[][] isn't, and i want to assign the values of secondStage to currentStage. So i created the function bellow:

现在我的secondStage [] []数组中填充了值,但currentStage [] []不是,我想将secondStage的值赋给currentStage。所以我创建了以下函数:

void populateArray(char (&arrayA)[mapXcor][mapYcor], char arrayB[mapXcor][mapYcor]) {
    for(int a = 0; a < mapXcor + 1; ++a) {
        for(int b = 0; b < mapYcor + 1; ++b) {
            arrayA[mapXcor][mapYcor] = arrayB[mapXcor][mapYcor];
        }
    }
}

populateArray(currentStage[mapXcor][mapYcor],secondStage[mapXcor][mapYcor]);

But when i use the function it gives me a error :

但是,当我使用该功能时,它给了我一个错误:

1 IntelliSense: a reference of type "char (&)[20][50]" (not const-qualified) cannot be initialized with a value of type "char"

1智能感知:类型为“char(&)[20] [50]”的参考(非const限定)无法使用“char”类型的值进行初始化

2 IntelliSense: argument of type "char" is incompatible with parameter of type "char (*)[50]"

2 IntelliSense:“char”类型的参数与“char(*)[50]”类型的参数不兼容

How could I fix this?

我怎么能解决这个问题?

3 个解决方案

#1


2  

change the assignment sentence in your for-loop:

更改for循环中的赋值语句:

arrayA[mapXcor][mapYcor] = arrayB[mapXcor][mapYcor];

by

arrayA[a][b] = arrayB[a][b];

#2


2  

Code you probably are trying to reach. Do not show it to teacher :-) - there is too much from practical programming. Just look and then write your own solutions:

您可能想要达到的代码。不要向老师展示:-) - 实际编程太多了。只需查看然后编写自己的解决方案:

#include <string.h>
#include <stdio.h>

#define mapXcor 5
#define mapYcor 5

static char secondStage    [mapXcor][mapYcor];
static char currentStage   [mapXcor][mapYcor];

void populateArray(char (&arrayA)[mapXcor][mapYcor], const char (&arrayB)[mapXcor][mapYcor]) {
/*
    for(int a = 0; a < mapXcor; ++a) {
        for(int b = 0; b < mapYcor; ++b) {
            arrayA[a][b] = arrayB[a][b];
        }
    }
*/
    memcpy(arrayA, arrayB, sizeof(arrayB));
}

int main()
{
    for(int a = 0; a < mapXcor; ++a) {
        for(int b = 0; b < mapYcor; ++b){
            currentStage[a][b] = a*b + 10*a;
        }
    }

    populateArray(secondStage, currentStage);

    for(int a = 0; a < mapXcor; ++a) {
        for(int b = 0; b < mapYcor; ++b){
            printf("%2i ", secondStage[a][b]);
        }
        printf("\n");
    }

    return 0;
}

Comments:

  • You have bad array index limits in cycle. Look for corrected function under comment.
  • 循环中的数组索引限制很差。在评论下寻找纠正的功能。

  • You have wrong indexes in assignment in cycle. Again, commented out code.
  • 循环中的赋值有错误的索引。再次,注释掉了代码。

  • If you passed one array by reference (destination) why not to use constant reference for source?
  • 如果您通过引用(目标)传递一个数组,为什么不对源使用常量引用?

  • In practice as your areas have the same type (you could even define them as having the same type) nobody copies them by elements. I have used memcpy() but this is only good if memory areas do not overlap (in this case - yes). check man 3 memcpy.
  • 实际上,由于您的区域具有相同的类型(您甚至可以将它们定义为具有相同类型),因此没有人按元素复制它们。我已经使用了memcpy(),但只有当内存区域不重叠时才这样做(在这种情况下是 - 是)。检查男人3 memcpy。

  • Learn harder :-)
  • 学习更难:-)

#3


0  

#define ROWS 2
#define COLS 2

void copyArray(char (&arrayA)[ROWS][COLS], const char (&arrayB)[ROWS][COLS] ) {

    memcpy(arrayA, arrayB, sizeof(arrayB));
}

int main(void){

    char arr1  [ROWS][COLS];
    char arr2  [ROWS][COLS];

    // init arr1
    copyArray(arr2, arr1);

    return 0;
}

#1


2  

change the assignment sentence in your for-loop:

更改for循环中的赋值语句:

arrayA[mapXcor][mapYcor] = arrayB[mapXcor][mapYcor];

by

arrayA[a][b] = arrayB[a][b];

#2


2  

Code you probably are trying to reach. Do not show it to teacher :-) - there is too much from practical programming. Just look and then write your own solutions:

您可能想要达到的代码。不要向老师展示:-) - 实际编程太多了。只需查看然后编写自己的解决方案:

#include <string.h>
#include <stdio.h>

#define mapXcor 5
#define mapYcor 5

static char secondStage    [mapXcor][mapYcor];
static char currentStage   [mapXcor][mapYcor];

void populateArray(char (&arrayA)[mapXcor][mapYcor], const char (&arrayB)[mapXcor][mapYcor]) {
/*
    for(int a = 0; a < mapXcor; ++a) {
        for(int b = 0; b < mapYcor; ++b) {
            arrayA[a][b] = arrayB[a][b];
        }
    }
*/
    memcpy(arrayA, arrayB, sizeof(arrayB));
}

int main()
{
    for(int a = 0; a < mapXcor; ++a) {
        for(int b = 0; b < mapYcor; ++b){
            currentStage[a][b] = a*b + 10*a;
        }
    }

    populateArray(secondStage, currentStage);

    for(int a = 0; a < mapXcor; ++a) {
        for(int b = 0; b < mapYcor; ++b){
            printf("%2i ", secondStage[a][b]);
        }
        printf("\n");
    }

    return 0;
}

Comments:

  • You have bad array index limits in cycle. Look for corrected function under comment.
  • 循环中的数组索引限制很差。在评论下寻找纠正的功能。

  • You have wrong indexes in assignment in cycle. Again, commented out code.
  • 循环中的赋值有错误的索引。再次,注释掉了代码。

  • If you passed one array by reference (destination) why not to use constant reference for source?
  • 如果您通过引用(目标)传递一个数组,为什么不对源使用常量引用?

  • In practice as your areas have the same type (you could even define them as having the same type) nobody copies them by elements. I have used memcpy() but this is only good if memory areas do not overlap (in this case - yes). check man 3 memcpy.
  • 实际上,由于您的区域具有相同的类型(您甚至可以将它们定义为具有相同类型),因此没有人按元素复制它们。我已经使用了memcpy(),但只有当内存区域不重叠时才这样做(在这种情况下是 - 是)。检查男人3 memcpy。

  • Learn harder :-)
  • 学习更难:-)

#3


0  

#define ROWS 2
#define COLS 2

void copyArray(char (&arrayA)[ROWS][COLS], const char (&arrayB)[ROWS][COLS] ) {

    memcpy(arrayA, arrayB, sizeof(arrayB));
}

int main(void){

    char arr1  [ROWS][COLS];
    char arr2  [ROWS][COLS];

    // init arr1
    copyArray(arr2, arr1);

    return 0;
}