Thread: Multi-dimensional Array in C

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Philippines
    Posts
    20

    Multi-dimensional Array in C

    Hello. Is it possible to create an 8 dimensional array in C?
    If no, then what can I do to improvise or let say bypass the limit?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Should be no problem. Go ahead and create it!

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Perhaps there's a better way to accomplish what you're doing. Would you care to explain what it is you want to do?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    AFAIK, there is no upper limit on array rank in C, but it may get confusing and cumbersome during coding to refer to or use it correctly.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Location
    Philippines
    Posts
    20
    I am trying to create an 8 dimensional character array. And store some letters in it.
    I tried 8 dimensional array (string). But compiler says "size of array 'letter' is too large". I am using DEV-C++ as my IDE and MinGW GCC 4.8.1 as my compiler. What should I do?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by itCbitC View Post
    AFAIK, there is no upper limit on array rank in C, but it may get confusing and cumbersome during coding to refer to or use it correctly.
    More or less. The memory usage of arrays with that many dimensions can easily be significant, and easily exceed the total addressable memory on modern machines. There aren't many ways around that.

    More generally, however, as Matticus gently hinted with a question, using multidimensional arrays is usually a sign of not having thought much about a design (for example, storing large amounts of data in memory in order to sift through it, rather than accessing data in smarter and more efficient ways from a file).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Approach the problem with a different algorithm. Please describe in words why you need an array with 8 dimensions to store characters. Individual characters or null terminated strings? What will the application do?

    The compiler should not be a factor.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why 8?

    char universe[COUNTRIES][CITIES][LIBRARIES][BOOKS][PAGES][PARAGRAPHS][SENTENCES][WORDLEN];
    None of these dimensions is anywhere near constant.
    Your array (assuming you can allocate it) will be full of huge holes where no data will be present.

    Something fairly trivial like char a[10][10][10][10][10][10][10][10]; takes up 100MB of memory.
    Just multiply all the dimensions together, and see how large the number is.

    Think about your data again, and look up what a "sparse datastructure" is.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Dimensional Array
    By johnmackin in forum C Programming
    Replies: 0
    Last Post: 06-13-2012, 06:45 PM
  2. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  3. multi-dimensional array
    By shuo in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2008, 01:03 AM
  4. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM
  5. multi-dimensional array
    By mcorn in forum C Programming
    Replies: 2
    Last Post: 08-04-2002, 09:14 AM