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. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    46

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    46
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21