Thread: initializing multi-dimensional arrays

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

    initializing multi-dimensional arrays

    Hi,
    I am confused about how multi-dimensional arrays are initialized. For instance, if I want to do something to the effect of:
    Code:
    int a[2][3];
    for (int i = 0; i < 2; ++i) {
            for (int r = 0; r < 3; ++r) {
                   a[i][r] = i+r;
            }
    }
    I would imagine that I can initialize a[2][3] like this:
    Code:
    int a[2][3] = {
            { 0, 1 },
            { 1, 2 },
            { 2, 3 }
    };
    since a[2][3] can be seen as "an array of 3 elements, each element is an array of 2 elements"?

    that is apparently incorrect as my compiler (gcc 4.1) gives me:
    Code:
    a.cpp:6: error: too many initializers for ‘int [2][3]’
    and writing the declaration as
    Code:
     int a[3][2];
    fixes it.

    Can you please point out what am I doing wrong?
    Thank you very much

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So it would be
    Code:
    int a[2][3] = {
            { 0, 1, 2 },  // 3 in each row, to match the minor dimension
            { 3, 4, 5 },  // 2 rows, to match the major dimension
    };
    You had a 3rd row in the "too many" case.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Thank you for your help.

    I guess there's something conceptually wrong with my understanding of multi-dimensional arrays then...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> since a[2][3] can be seen as "an array of 3 elements, each element is an array of 2 elements"?

    a[2][3] should be seen as an array of 2 elements, each of which is an array of 3 elements. Just make that simple switch in your head and you should be good.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    a[2][3] should be seen as an array of 2 elements, each of which is an array of 3 elements. Just make that simple switch in your head and you should be good.
    Thank you for your reply. I think I get it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. fscanf for multi dimensional arrays
    By rambos in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 03:26 AM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM