简体   繁体   English

将一维数组插入二维数组

[英]Inserting 1D array to 2D array

Can we directly insert a 1D array to a 2D array?我们可以直接将一维数组插入二维数组吗?

For example I have this code:例如我有这个代码:

void insert(int[]data , int**collection)
{
collection[1] = data
}
int main()
{
int data[2]= {1,3}
int collection[2][2];
insert(data,&collection);
}

Will this work?这行得通吗?

You cannot insert a 1D array to 2D array the way you are doing.您不能按照您的方式将一1D数组插入2D数组。 Use memcpy to copy the elements of 1D array to 2D array, like this:使用memcpy1D数组的元素复制到2D数组,如下所示:

memcpy(collection[1], data, 2 * sizeof(int));

this will copy the 2 integer elements of data array to collection[1] .这会将data数组的2 integer 元素复制到collection[1]

Besides, a couple of problems in your code.此外,您的代码中存在一些问题。 Lets discuss them:让我们讨论它们:

First:第一的:

insert(data,&collection);
            ^

You don't need to pass the address of collection .您不需要传递collection的地址。 Note that, an array, when used in an expression, will convert to pointer to its first element (there are few exceptions to this rule).请注意,当在表达式中使用数组时,将转换为指向其第一个元素的指针(此规则很少有例外)。 That means, when you pass collection , it will convert to type int (*)[2] .这意味着,当您传递collection时,它将转换为类型int (*)[2] Just do:做就是了:

insert(data, collection);

Second:第二:

void insert(int[]data , int**collection)

int[]data is wrong. int[]data错误。 The first parameter of insert() should be int data[2] or int data[] , both are equivalent to int * data . insert()的第一个参数应该是int data[2]int data[] ,两者都等价于int * data You can use either of them.您可以使用其中任何一个。

The second argument to insert() is collection array which is a 2D array of integers. insert()的第二个参数是collection数组,它是一个2D整数数组。 When you pass it to insert() , it will decay to pointer whose type is int (*)[2] .当您将其传递给insert()时,它将衰减为类型为int (*)[2]的指针。 The type of second parameter of insert() is int ** which is not compatible with the argument that you are passing to insert() function. insert()的第二个参数的类型是int ** ,它与您传递给insert() function 的参数不兼容。 The second parameter of insert() function should be int collection[2][2] or int collection[][2] , both are equivalent to int (*collection)[2] . insert() function 的第二个参数应该是int collection[2][2]int collection[][2] ,两者都等价于int (*collection)[2]

Putting these altogether, you can do:把这些放在一起,你可以这样做:

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

#define ROW 2
#define COL 2

void insert(int data[ROW], int collection[ROW][COL]) {
    //for demonstration purpose, copying elements of data array 
    //to all elements (1D array) of collection array.
    for (int i = 0; i < ROW; ++i) {
        memcpy(collection[i], data, COL * sizeof(int));
    }
}

int main(void) {
    int data[COL] = {1, 3};
    int collection[ROW][COL];

    insert(data, collection);

    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            printf("collection[%d][%d] : %d ", i, j, collection[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output: Output:

# ./a.out
collection[0][0] : 1 collection[0][1] : 3 
collection[1][0] : 1 collection[1][1] : 3 

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

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