简体   繁体   中英

creating 2d array from 1d arrays

if i have several arrays of the same datatype, what is the best way to copy them all into a 2d array. for example

int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {9,8,7,6,5,4,3,2,1,0};

int array2d[][];
//pseudo code array2d = array1 + array2

so that

array2d[0][0]; //=1 (first member of array1)
array2d[1][0]; //=9 (first member of array2)

considering an array is just a pointer to the first element, i thought I could do this, but it creates a compiler error.

array2d[0][0] = array1;
array2d[1][0] = array2;

I'm guessing I can't copy using references because an array needs its entries in contiguous memory? is there a memset like funciton I can use?

Impossible. You need to copy element by element from one array to another.

Also you can mimic 2d array with array of pointers to arrays of ints.

int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {9,8,7,6,5,4,3,2,1,0};

int *array2d[2]; 

array2d[0] = array1;
array2d[1] = array2;

or this

int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {9,8,7,6,5,4,3,2,1,0};

int *array2d[] = {array1, array2}; 

cout << "[0][0]=" << array2d[0][0] << endl;
cout << "[1][0]=" << array2d[1][0] << endl;

OR REVERSE

If your goal is to present 2d array to some API, then you should refactor your side. For example, you can mimic your 1d arrays with pointers:

// an ampty array
int array2d[2][10];

// pointers to parts
int *array1 = array2d[0];
int *array2 = array2d[1];

int n;

// fill "arrays"
for(int i=0, n=1; i<10; ++i, ++n) {
    array1[i] = n;
}
for(int i=0, n=9; i<10; ++i, --n) {
    array2[i] = n;
}

// now you are ready
cout << "[0][0]=" << array2d[0][0] << endl;
cout << "[1][0]=" << array2d[1][0] << endl;

If you want an actual 2D array (contiguous in memory), you'll have to copy the elements. However, you could emulate it with an array of 2 pointers:

int *array2d[2];
array2d[0] = array1;
array2d[1] = array2;

It's not an actual 2D array, but you could make array2d an array of pointers to the 1d arrays:

int* array2d[2];

array2d[0] = array1;
array2d[1] = array2;

Otherwise you'll have to copy the elements over manually.

There's a few things that you can do. If you know that your data is going to be constant within each array, you can #define it, and then use it in your 1D and 2D arrays. Alternatively, you can memcpy the elements from the 1D array to the 2D array. The second point is illustrated here:

#define ARRAY_1 { 1, 2, 3, 4, 5, 6 }
#define ARRAY_2 {7, 8, 9, 10, 11, 12 }

int array_1[] = ARRAY_1;
int array_2[] = ARRAY_2;

int two_dim_array[][] = {
    ARRAY_1,
    ARRAY_2,
}

Define and assign :

int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[] = {9,8,7,6,5,4,3,2,1,0};

int *array2d[2];
array2d[0] = array1;
array2d[1] = array2;

Test :

printf("%d\t",array2d[0][0]);
printf("%d\t",array2d[0][9]);
printf("%d\t",array2d[1][5]);

it gives 1 10 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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