Thread: How does multidimensional array store variables

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    40

    How does multidimensional array store variables

    I am having trouble to understand How does multidimensional array store variables

    No problem with 1D array
    Code:
     int one_dim[] = { 1,2,3 };
    My understanding for 2D
    Code:
     #define row    3
    #define coulmn  4
    
    int two_dim[row][coulmn] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} };
    I am having trouble to understand N D array

    Code:
     int N_dim[2][3][4][5];
    any idea how to remember

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,328
    The right-most dimension varies quickest.

    Or you need as many initial opening braces as you've got dimensions.
    Code:
    int one_dim[4] = { 1,2,3,4 };
    int two_dim[3][4] = {
      { 1,2,3,4 },
      { 1,2,3,4 },
      { 1,2,3,4 },
    };
    int three_dim[2][3][4] = {
     {
      { 1,2,3,4 },
      { 1,2,3,4 },
      { 1,2,3,4 },
     },
     {
      { 1,2,3,4 },
      { 1,2,3,4 },
      { 1,2,3,4 },
     },
    }
    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
    Apr 2021
    Posts
    98
    It might help if you really, really learn how C declarations work.

    Consider this:
    Code:
        int ary[3][5];
    In C, the rule is that type operators (pointer, array, function) are read postfix, then prefix, starting with the declarator (if there is one). The C operator precedence supports this.

    This means, in our example, that we read it like:


    1. ary is an
    2. array-of-3
    3. array-of-5
    4. int


    The important thing is that because the declaration and evaluation of identifiers uses the same structure, when you understand how to read and write them, you understand how to look at partial pieces of them. Consider this:

    Code:
        a[1]
    What's the type of a[1]? Well, you can replace it in the declaration, and then read it in the same old way: postfix, then prefix, then type specifiers:


    1. Ay_sub_won is an
    2. array of 5
    3. int


    Why does this matter? Because "objects" in C represent a single block of memory! Meaning that a[1] is an "object" that occupies a block of memory that is stored immediately after a[0] and immediately before a[2]. Furthermore, you know that a[1][1] is stored right before a[1][2]. With this information alone, you should be able to build a "map" of where the objects are in memory.

    To boil it down, a 2-d array is like a column of text: the dimension closest to the variable name (a[1]) selects the "row" to read, and then the dimension farther away selects the item within the row (the "column"). This is known as "row-major order" (as opposed to "column-major" order).

    Similarly a 3-d array is like a stack of papers. The first dimension selects the page, the second dimension selects the row of text, the third dimension selects the character within the row.

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    40
    Quote Originally Posted by Salem View Post
    The right-most dimension varies quickest.

    Or you need as many initial opening braces as you've got dimensions.
    I understand following declarations
    Code:
    int one_dim[4] ; // It can store 4 variables 
    int two_dim[4][4] ; // It can store 16 variables 
    int three_dim[2][3][4] ;// it can store 24 variables
    How many variable can store following declarations ?

    Code:
     int three_dim[2][3][4][5]

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,328
    You just multiply all the dimensions
    Code:
    int one_dim[4] ; // It can store 4 variables = 4
    int two_dim[4][4] ; // It can store 16 variables = 4 * 4
    int three_dim[2][3][4] ;// it can store 24 variables = 2 * 3 * 4
    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. Replies: 10
    Last Post: 10-14-2013, 11:56 AM
  2. Replies: 6
    Last Post: 08-15-2010, 07:12 AM
  3. Store variables in another file
    By DarkAlex in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2007, 10:34 PM
  4. global variables and multidimensional arrays
    By LowLife in forum C Programming
    Replies: 12
    Last Post: 11-18-2005, 04:02 PM
  5. where does the compiler store the local variables?
    By debuger2004 in forum C Programming
    Replies: 1
    Last Post: 06-23-2003, 08:19 PM

Tags for this Thread