Multidimensional Arrays

Discussion in 'C Programming' started by Terry, Aug 30, 2003.

  1. Terry

    Terry Guest

    Hi,

    can someone plz tell me how multidimensional arrays (like a 2-D array)
    are stored in memory? Are they like single dimensional arrays? Stored
    sequentially in one "row", so to say?

    Thanks

    Terry
     
    Terry, Aug 30, 2003
    #1
    1. Advertising

  2. Terry

    Mike Wahler Guest

    Terry <> wrote in message
    news:3f509baa$...
    > Hi,
    >
    > can someone plz tell me how multidimensional arrays (like a 2-D array)
    > are stored in memory?


    C doesn't really have 'multidimensional' arrays. What
    looks like one is really an 'array of arrays'.

    Arrays (however many 'dimensions') are stored in contigous
    memory locations.

    > Are they like single dimensional arrays? Stored
    > sequentially in one "row", so to say?


    An array:

    int arry[2][3];

    is stored thus:

    Low High
    address address
    [0][0], [0][1], [0][2], [1][0], [1][1], [1][2]

    -Mike
     
    Mike Wahler, Aug 30, 2003
    #2
    1. Advertising

  3. Terry wrote:
    >
    > Hi,
    >
    > can someone plz tell me how multidimensional arrays (like a 2-D array)
    > are stored in memory? Are they like single dimensional arrays? Stored
    > sequentially in one "row", so to say?
    >
    > Thanks
    >
    > Terry


    C and most modern languages store 2D arrays "row-wise". That is, if
    S is the data size in storage units, and W is the number of items in
    a row, the actual address of the i,j 'th item is

    A[0][0] + S*(i+W*j)

    of course a real C maven would express the above differently,
    probably with pointers, but you see what I mean.

    Fortran, BTW, stores data column-wise. This is a problem for people
    who have to link compiled Fortran library functions to C or vice
    versa.



    --
    Julian V. Noble
    Professor Emeritus of Physics

    ^^^^^^^^^^^^^^^^^^
    http://galileo.phys.virginia.edu/~jvn/

    "Science knows only one commandment: contribute to science."
    -- Bertolt Brecht, "Galileo".
     
    Julian V. Noble, Aug 30, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. d[ - - ]b

    Multidimensional arrays? anything else?

    d[ - - ]b, May 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,684
    AlexS
    May 18, 2004
  2. Jay
    Replies:
    1
    Views:
    2,622
    BarryNL
    Jan 30, 2004
  3. geclinke
    Replies:
    1
    Views:
    5,131
    jackie
    Jun 18, 2004
  4. Philipp
    Replies:
    21
    Views:
    1,184
    Philipp
    Jan 20, 2009
  5. Francesco
    Replies:
    2
    Views:
    1,166
    Francesco
    Nov 6, 2009
Loading...

Share This Page