Thread: How can I copy a 3 dimensional array into a 2 dimensional array?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    9

    How can I copy a 3 dimensional array into a 2 dimensional array?

    I have a 3D array that contains 200 strings. I'm trying to copy all these strings into a 2D array. How can this be done?
    This is what I have so far but it isn't working correctly.

    Code:
    for(int i = 0; i < row; i++)     { 
          for (int j = 0; j < col; j++)
           {
             dest[i][j] = source[0][i][j];
              
           }      }
    
    The finished product would be with 100 rows, 2 columns.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to show us how source and dest are declared.
    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 2013
    Posts
    9
    Code:
    char dest[108][26]
    char source[5][42][13]

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It might help if your arrays matched the words in your first post.

    source isn't 200 of anything, and dest isn't 100 of anything either.

    To copy a single string, it would be something like
    strcpy(dest[i],source[0][i]);
    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.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    9
    Code:
    char dest[100][2]
    char source[5][2][20]
    I 'm making my our strcpy function btw.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 09-02-2013, 07:50 PM
  2. 2 dimensional array
    By zhangz64 in forum C Programming
    Replies: 8
    Last Post: 05-12-2013, 01:21 AM
  3. Replies: 3
    Last Post: 06-10-2011, 07:47 PM
  4. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM