Closed Thread
Results 1 to 4 of 4

Thread: Initializating multidimensional arrays

  1. #1
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0

    Initializating multidimensional arrays

    I must admit that the concept of multidimensional arrays and the use of pointers in C is giving me quite a headache in many of my projects.

    Say, I want a 3-dimensional array with 3 entries in each dimension, i.e. 27 entries total. I want to run through the first dimension, and use curly brackets to initialize all entries in that dimension. But not using for loops. Something like this:

    byte cube[3][3][3];
    cube[0] = {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}};
    cube[1] = {{1, 1, 1}, {0, 0, 1}, {0, 1, 0}};
    cube[2] = {{0, 1, 1}, {1, 0, 0}, {0, 1, 1}};

    Of course this will not work. But it shows the main idea of what I want to do. Initializing a 3-dimensional array using curly brackets and no for loops. How could I do this exactly?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Initializating multidimensional arrays

    byte cube[3][3][3] ={{{0, 0, 0}, {1, 0, 1}, {1, 1, 0}},{{1, 1, 1}, {0, 0, 1}, {0, 1, 0}},{{0, 1, 1}, {1, 0, 0}, {0, 1, 1}}};
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0

    Re: Initializating multidimensional arrays

    Actually, I found out just the way to do it.

    Code:
    byte cube[3][3][3] = {[0] = {{0, 0, 0}, {1, 0, 1}, {1, 1, 0}},
    [1] = {{1, 1, 1}, {0, 0, 1}, {0, 1, 0}};
    [2] = {{0, 1, 1}, {1, 0, 0}, {0, 1, 1}}};
    This is useful because I do not want to fill out all entries in my actual array. However, this poses a new problem for me.

    I apparently need to declare and initialize my array in the same line. I want to do this in a function in one C file. However, I need access to the array from another C file. How would I go about doing that, since the array is only declared in the function's scope?

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Initializating multidimensional arrays

    You can pass a pointer to the array, but then you will need to use some form of loop to initialize it.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Problem with multidimensional arrays and foreach
    By DEViANT in forum PHP Development
    Replies: 1
    Last Post: 08-22-2010, 08:31 AM
  2. Replies: 7
    Last Post: 07-10-2010, 02:42 AM
  3. Replies: 2
    Last Post: 11-02-2009, 05:59 AM
  4. Multidimensional Arrays
    By chili5 in forum Java Tutorials
    Replies: 22
    Last Post: 08-14-2009, 09:02 AM
  5. Returning multidimensional arrays from function
    By ThemePark in forum C and C++
    Replies: 11
    Last Post: 12-01-2008, 07:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts