0

If the array is initialized like this:

int a[4][3]={1,2,3,4,5,6,7,8,9};

Could someone please help me how the elements are stored in array indices, like a[0][0] will be which element and so on

H.S.
  • 11,654
  • 2
  • 15
  • 32

3 Answers3

1

Statement:

int a[4][3]={1,2,3,4,5,6,7,8,9};

is equivalent to:

int a[4][3] = {
                {1,2,3},  //every row contains 3 element because second dimension is 3
                {4,5,6},
                {7,8,9}
              };

With this way of representation, you can easily identify the value at a particular location in the given array.

a[0][0] will be which element and so on

a[0] is {1,2,3} and in this array the element at 0th location is 1. So,

a[0][0] is 1, Similarly
a[0][1] is 2
a[0][2] is 3
a[1][0] is 4
and so on.

May you write a program for better understanding, like this:

#include <stdio.h>

int main() {
    int a[4][3]={1,2,3,4,5,6,7,8,9};
    int i,j;

    for (i = 0; i < 4; i++) {
        for (j = 0; j < 3; j++) {
            printf ("a[%d][%d] : %d\n", i, j, a[i][j]);
        }
    }
    return 0;
}

The output of this program is:

a[0][0] : 1
a[0][1] : 2
a[0][2] : 3
a[1][0] : 4
a[1][1] : 5
a[1][2] : 6
a[2][0] : 7
a[2][1] : 8
a[2][2] : 9
a[3][0] : 0
a[3][1] : 0
a[3][2] : 0

Here you can see the last row (a[3]) is having all elements value 0. This is because in array initialization if there are fewer initializers than elements in the array then, remaining elements are automatically initialized to 0. Since you are providing only 3 rows in the initialization of the array of 4 rows, the elements of the last row will be initialized with 0.
The array a is:

1 2 3 
4 5 6 
7 8 9 
0 0 0
H.S.
  • 11,654
  • 2
  • 15
  • 32
0

A multi-dimensional array store arrays.Suppose you have arr[4][3],this means that that array "arr" is consist of 4 arrays, each of length you specified in the second bracket.

Also in order to assign multi-dimensional array you should do it like this

   int arr[size1][size2] = {{contents of first},{content of second},{content of third},{etc}};

arr[0][0] will contain element located at first index of first array

shobhit
  • 91
  • 3
  • 12
-1

mapping multi-dimensional data on it can be done in several ways. there are mainly two types of data store procedure in 2D Array.

1.Row Major

Computer memory will be represented as a linear array with low addresses on the left and high addresses on the right. Also, we're going to use programmer notation for matrices: rows and columns start with zero, at the top-left corner of the matrix. Row indices go over rows from top to bottom; column indices go over columns from left to right.

As mentioned above, in row-major layout, the first row of the matrix is placed in contiguous memory, then the second, and so on.

Image of Row Major :-- Click Below

Array & Memory Representation of Storing Data in 2D Array by Row Major

2.Column Major

Describing column-major 2D layout is just taking the description of row-major and replacing every appearance of "row" by "column" and vice versa. The first column of the matrix is placed in contiguous memory, then the second, and so on:

Image of Column Major:--- Click Below

Array & Memory Representation of Storing Data in 2D Array by Column Major

I hope this will help you to understand how the value stored in araay.

Harsh Bhut
  • 238
  • 2
  • 14
  • None of this is relevant in C, where there aren't really "2D" arrays -- just arrays of arrays, – Chris Dodd Feb 06 '18 at 18:18
  • you are right there are really not a 2d array or 3d array in C or there are just arrays of arrays but for simply understanding the answer we used to call it 2d array. regarding this question he want simple understanding how the array represent works in memory so i put these type of answer. – Harsh Bhut Feb 07 '18 at 04:51