Convert a two-dimensional array to a one demensional array

This is a discussion on Convert a two-dimensional array to a one demensional array within the C Programming forums, part of the General Programming Boards category; Well the title pretty much explains itself...anyone know how to do this?...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    48

    Convert a two-dimensional array to a one demensional array

    Well the title pretty much explains itself...anyone know how to do this?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes. Pointer arithmetic and/or cast

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    so if i had an array for example power[8][7], i would cast it how?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    power1D[56] == power2D[8][7]

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course you can't assign arrays-as-a-whole. But you could do int *power1D=power2D[8][7], and pretend that power1D has 56 elements.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    so it must have that pointer

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could use a real array if you want, instead of a pointer. But you'd have to use a big ol' for-loop to get all the numbers in the new array.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    ok ill go with the pointer

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    As long as you access the 2D array elements in their proper storage order you can use either a pointer with an offset or a subscripted array.
    The thing to remember is to walk along the 2D array correctly knowing that the rightmost subscript varies fastest.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    so does this work NROWS being 8 and NCOLS being 7
    Code:
    int i, j, k;
    int power1d[56];
    
    for(i = 0; i<NROWS; i++)
    {
    for(j=0; j<NCOLS; j++)
    {
    for(k=0; k <56; k++)
    power1d = power[i][j];
    }}

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by belkins View Post
    so does this work NROWS being 8 and NCOLS being 7
    Code:
    int i, j, k;
    int power1d[56];
    
    for(i = 0; i<NROWS; i++)
    {
    for(j=0; j<NCOLS; j++)
    {
    for(k=0; k <56; k++)
    power1d = power[i][j];
    }}
    No. You can never assign to the name of an array. Fortunately, you don't want to assign the name of an array, but to a particular slot in the array.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    does another array need to be declared?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by belkins View Post
    does another array need to be declared?
    Of course not.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    48
    do i declare the 2-d array as one-d then

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by belkins View Post
    do i declare the 2-d array as one-d then
    Well, wait. I thought you meant another another array. The one one-d array you already have is fine. But you have to assign to array[something], not just array.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 04:28 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. convert long to pointer to char array
    By gazmack in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2003, 11:33 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