Array -> 2D array

This is a discussion on Array -> 2D array within the C Programming forums, part of the General Programming Boards category; How can I convert an array of 25 elements into a 5x5 2D array?...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    22

    Array -> 2D array

    How can I convert an array of 25 elements into a 5x5 2D array?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Create 2D array, then loop through start to end and set individuals elements.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,047
    You could cast the addresses:
    Code:
    typedef int array2d [5][5];
    
    *(array2d *)&myArray1D;
    Or without typedefs:
    Code:
    *(int (*)[5][5])&myArray1D
    Or if passed to a function, simply cast the 1D array to void pointer.

    But if you need a 2d array, why do you have a 1D array? You should probably be loading your data directly into a 2d array in the first place.
    Last edited by King Mir; 05-15-2008 at 01:07 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    But if you need a 2d array, why do you have a 1D array? You should probably be loading your data directly into a 2d array in the first place.
    You never 'need' a 2D array per se.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bubba View Post
    You never 'need' a 2D array per se.
    What bubba means is that you can make any dimension array using math: The index of an X-dimension array is:
    Code:
    index = p0 + p1 * n0 ... pX *n(X-1) * n(X-2) ... n0;
    where p{0..X} is the position of that dimension, and n{0..X} is the size of that dimension.

    Example:
    Code:
    // Treat a 1d 1000 element array as a 3D [10][10][10] array. 
    int array[1000];  
    int x, y, z;
    for(x = 0; x < 10; x++)
    {
       for(y = 0; y < 10; y++)
       {
          for(z = 0; z < 10; z++)
          {
              array[x + (y * 10) + (z * 100)] = x * y * z;
          }
       }
    }
    This will be exactly what the compiler does internally anyways, when it deals with multidimensional arrays.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  2. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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