Thread: Multidementional arrays, need concept explanation??

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    103

    Multidementional arrays, need concept explanation??

    Hi everyone,
    today, My C prof covered multidimensional arrays...he says that, there exist, in addition to the two dimensional arrays, n-dimensional arrays.. he asks us not to pay attention to these concepts and focus only on the two dimensional model..
    The problem is that i want to go deeper in C, deeper than the school program, and i guess it's crucial to understand what means to have more than two dimensional array..
    PS: i can imagine what is gonna be like a three-dimensional array in reality...but how that works in memory??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can have as many dimensions as you like [1]:

    Code:
    int onedim[10];
    int twodim[10][10];
    int threedim[10][10][10];
    int fourdim[10][10][10][10];
    int fivedim[10][10][10][10][10];
    I'll leave it to you to figure out what sixdim and tendim would look like.

    And tendim might struggle to fit in a 32-bit machine - care to guess why?

    [1] I don't know if the C standard has a specific limit for what the compiler must cope with - if there is such a limit, it's still allowed for the compiler to allow more dimensions, if it likes to.

    Edit: I missed your last question. The compiler will translate ANY multidimensional array into a block of memory that is the right size to cope with the data. How that's organized is up to the compiler, but generally it's just like a one dimensional array with index calculation like this:
    n0 + n1 * n0_max + n2 * (n0_max * n1_max) ...

    --
    Mats
    Last edited by matsp; 04-29-2008 at 08:07 AM.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    103
    Ok, so you wanna say that the compiler breaks everything down to only two dimensions..??

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    the compiler breaks all multidimensional arrays to one dimension, not two. So it's just a bunch of array elements in a row, and it calculates the position you want.
    Last edited by robwhit; 04-29-2008 at 11:07 AM.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    very interesting, I to was wondering this some time back. It's good to have an insight.

    n0 + n1 * n0_max + n2 * (n0_max * n1_max) ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Multidementional arrays
    By kingneb in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2008, 03:24 PM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM