# copying a 2D array into another 2D array

This is a discussion on copying a 2D array into another 2D array within the C Programming forums, part of the General Programming Boards category; Hi, I have a 2 dimensional array. I know the size of the second dimension but not the first one ...

1. ## copying a 2D array into another 2D array

Hi,

I have a 2 dimensional array. I know the size of the second dimension but not the first one i.e. number of columns is known but the number of rows is not known. In such situation, I can declare the array like this:

Code:
`int array[][4];`
and assign to it the values like this:

Code:
```int array[][4] = {
{11, 2, 3, 0},
{2, 4, 5, 0},
{4, 0, 0, 0},
{5, 0, 0, 0},
{3, 6, 22, 0},
{6, 0, 0, 0},
{22, 0, 0, 0}
};```

My question is, if I need to copy the content of the above array into another array. How do I do this, i.e. the bytes to copy.

Let us say, I have:

Code:
```int dest_array[][4];
memcpy(dest_array, array, (?) * 4 * sizeof(int));```
How do I handle this. Should I keep track of number of rows, can I specify a safe big number i.e. 20 (instead of 7 as in the above example) and hope that the array is copied safely ?

Thanks,

2. Well the first thing to do is to make sure your dest array is at least as big as your source array.

For this, you will need something like
int dest_array[20][4];

The memcpy would work, but you need to copy min(sizeof(dest_array),sizeof(array)), where both arrays are in scope.

> and assign to it the values like this:
That's actually initialisation, not assignment.
You can initialise arrays in C, you can't assign them (in the array2 = array1 sense)

3. Before the array is sent into another function (and decays into having a sizeof(pointer to data type), get the sizeof(array). Now use your sizeof(data type), to calculate the number of rows, since you know the number of columns already.

That's a bad description perhaps. Let's look at an example with crackers.

Each cracker is 5cm let's say, and each row has 10 crackers in it.

If you knew that the entire array of crackers had a size of 500 cm's, you could use a bit of arithmetic and figure out how many rows of crackers you had.

4. Hi Salem,

thanks,

ok, so if in my dest_array, I specify the size of first dimension as 20. i.e., int dest_array[20][4];

then,

(A)
can I leave my source array's first dimension as unspecified i.e.
int source_array[][4];
and do
memcpy(dest_array, source_array, min(sizeof(dest_array),sizeof(source_array))

(B)
or, shall I completely specify my source array as
int source_array[7][4];
and do,
memcpy(dest_array, source_array, min(sizeof(dest_array),sizeof(source_array))

(C)
Concerning, both arrays should be in scope.
my destination array is defined in a different file, will that be a problem with the aforementioned approach.

thanks,

5. > can I leave my source array's first dimension as unspecified i.e.
ONLY if the size is determined by the initialisation which follows.

> my destination array is defined in a different file, will that be a problem
> with the aforementioned approach.
Well you will need to pass the array size(s) as parameters to whatever function is doing the work.