Thread: Two Dimensional Arrays

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    New Delhi, India
    Posts
    3

    Two Dimensional Arrays

    I've just begun C++; Looking through arrays, there's just this text in bold that I'm not totally clear about. It would be nice if someone could explain it a bit more clearly.

    The following is a quote from "C++ From The Ground Up" by Herbert Schildt
    Two-dimensional arrays are stored in a row-column matrix, where the first index indicates the rown and the second indicates the column. This means that when array elemets are accessed in the order in which they are actually stored in memory, the right index changes faster than the left.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    First, you should throw away that book. Herbert Schildt is generally shunned by the community. See the book recommendations thread for details.

    When you have a 2d array:
    Code:
    int ar[4][4];
    then it is laid out in memory as a single block of memory. Since memory is one-dimensional, there must be some mapping. The layout is:
    Code:
    ar[0][0] | ar[0][1] | ar[0][2] | ar[0][3] | ar[1][0] | ... | ar[3][3]
    So when you go through the memory in order, the right-most index of the array element there changes the fastest.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    New Delhi, India
    Posts
    3
    Oh great. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  3. Two dimensional arrays
    By Masschino in forum C Programming
    Replies: 9
    Last Post: 05-18-2004, 08:17 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM