Thread: copying a 2D array into another 2D array

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    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. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying an array into another
    By Alienchicken in forum C Programming
    Replies: 4
    Last Post: 04-16-2011, 02:41 PM
  2. Replies: 3
    Last Post: 03-11-2011, 02:49 AM
  3. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  4. copying values of an array to another array?!
    By webznz in forum C Programming
    Replies: 8
    Last Post: 10-24-2007, 10:59 AM
  5. Copying array's
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 01:25 PM