3

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?

cool mr croc
  • 725
  • 1
  • 13
  • 33
  • 6
    "considering an array is just a pointer to the first element"... nah, nah, nah, an array is not a pointer to the first element, which explains why it does not work (further reading: http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c and http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c). – R. Martinho Fernandes Dec 18 '12 at 15:24

5 Answers5

3

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;
Dims
  • 47,675
  • 117
  • 331
  • 600
2

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;
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

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.

WildCrustacean
  • 5,896
  • 2
  • 31
  • 42
  • thats how i would normally do it but its to send to an api function expecting an array like '**datatype' – cool mr croc Dec 18 '12 at 15:28
  • does the API expect `**datatype` or `datatype[][]`? You should probably read the question linked in the comments, there is a lot of good information there. – WildCrustacean Dec 18 '12 at 15:31
1

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,
}
1

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

Timothy
  • 4,467
  • 5
  • 28
  • 51