Hello/Hi,
I m new to this pool of C.
I wass just reading a book named "C TUTOR" this book is gr8..
but i cannot understand the subscripts of 2D arrays...
I find them pretty hard..
Plz tell me wat exactly the below programme mean of suggest me some simple books or sites...
Plz simple and breif ...
Thanks in advance..

main( )
{
      int i,j;
      int big[8][8],huge[25][12];
      for (i = 0;i < 8;i++)
            for (j = 0;j < 8;j++)
                   big[i][j] = i * j; /* This is a multiplication table */
      for (i = 0;i < 25;i++)
              for (j = 0;j < 12;j++)
                    huge[i][j] = i + j; /* This is an addition table */

big[2][6] = huge[24][10] *22;
big[2][2] = 5;
big[big][2][2]...big[2][2]. = 177; /* this is big[5][5] = 177; */

for (i = 0;i < 8;i++) {
                 for (j = 0;j < 8;j++)
                             printf("%5d ",big[i][j]);
                                printf("\n"); /* newline for each increase in i */
     }
}

This programme shows no errors and works perfectly but i cannot understand the mechanism behind that plz help me...
Its urjent....
plz,
Tell me the entire programme function...
Thnx....

Salem commented: 10 posts, no code tags, COOKIE TIME!!!!! -4

Dani AI

Generated

’s checkers‑board analogy is a good starting point: a 2D array in C is an array of rows, and the first subscript picks the row while the second picks the column. C stores arrays in row‑major order, so elements in the same row and consecutive columns sit next to each other in memory. That row‑major fact explains how indexing maps to a single linear offset.

Address math (useful to understand what the compiler does): the address of A[i][j] equals the base address plus ((i NUM_COLS) + j) times the element size. For example, for an int big[8][8], the element big[2][6] is the 22nd int after the start because (28)+6 = 22. That explains why using an array element as an index only works if that element actually holds a valid index (and is within bounds).

A short demonstration of the pointer/offset equivalence:

#include <stdio.h>

int main(void)
{
    int A[3][4];
    size_t ROWS = sizeof A / sizeof A[0];
    size_t COLS = sizeof A[0] / sizeof A[0][0];

    for (size_t r = 0; r < ROWS; ++r)
        for (size_t c = 0; c < COLS; ++c)
            A[r][c] = (int)(r * COLS + c);

    int r = 2, c = 1;
    int by_index   = A[r][c];
    int by_pointer = *(*(A + r) + c);   /* same value */
    size_t byte_offset = (r * COLS + c) * sizeof A[0][0];

    (void)by_index; (void)by_pointer; (void)byte_offset;
    return 0;
}

The garbled expression in the original post was the confusing part. correctly inferred the intent: using an element value as an index (e.g. big[ big[2][2] ][ big[2][2] ] = 177;) is valid only if big[2][2] was set earlier and is inside 0..7. Writing big[big] is nonsense and won’t compile: the array name decays to a pointer and cannot be used as an integer index. and are right about readability—consistent indentation and avoiding magic numbers (use named constants or sizeof tricks) prevent mistakes and make bounds errors easier to spot.

Recommended Answers

All 4 Replies

Think of a 2 dimensional array like a checkers board, which has rows and columns of squares. Or you could think in terms of an electronic spreadsheet. In a 2d array the first dimension represents the rows and the second dimension represents the columns.

You might declare a spreadsheet that has 10 rows of 3 columns each like this:

int array[10][3];

and to reference row 3 column 2 array[3][2] = 0; That's really all there is to it. Just remember that C language counts rows and columns starting with 0, not 1, so the columns are numbered 0, 1 and 2 and the rows are 0, 1, 2, ... 9.

If you are confused about the following line, don't be. All of us including the compiler should be confused about it since it has syntax error and the comments don't match either big[big][2][2]...big[2][2]. = 177; /* this is big[5][5] = 177; */ :)

On more serious note let's see your code

for (i = 0;i < 8;i++)
   for (j = 0;j < 8;j++)
      big[i][j] = i * j; /* This is a multiplication table */

This multiplies the indices i and j, and stores the result in the big[j] location. For example if i = 2, and j=3, then i*j = 6 so it will assign 6 to the array element big[2][3].

for (i = 0;i < 25;i++)
   for (j = 0;j < 12;j++)
      huge[i][j] = i + j; /* This is an addition table */

This does the same thing except it adds instead of multiplying. big[2][6] = huge[24][10] *22; This multiplies the value stored in huge[24][10] by 22 and stores it in big[2][6] location. The value of huge[24][10] should be 24+10 = 34 because it was computed in the second set of nested loops above by adding the indices together. So multiply it by 22 you get 34*22 = 748. This gets stored in big[2][6].

Oh now that I look at the code where the error is, the ... must have been a comma ][ so assuming that's the case the statement should look like

big[2][2] = 5;
big[ big][2][2] ][ big[2][2] ] = 177; /* this is big[5][5] = 177; */

Now pay close attention to the way the statement is written. Notice that it is using big[2][2] as index for both dimensions and the value of big[2][2] is set to 5 in the statement before it. So it is essentially doing big[5][5] = 177

Hope this helps.

Hello/Hi,
I m new to this pool of C.
I wass just reading a book named "C TUTOR" this book is gr8..
but i cannot understand the subscripts of 2D arrays...
I find them pretty hard..
Plz tell me wat exactly the below programme mean of suggest me some simple books or sites...
Plz simple and breif ...
Thanks in advance..

main( )
{
      int i,j;
      int big[8][8],huge[25][12];
      for (i = 0;i < 8;i++)
            for (j = 0;j < 8;j++)
                   big[i][j] = i * j; /* This is a multiplication table */
      for (i = 0;i < 25;i++)
              for (j = 0;j < 12;j++)
                    huge[i][j] = i + j; /* This is an addition table */

big[2][6] = huge[24][10] *22;
big[2][2] = 5;
big[big][2][2]...big[2][2]. = 177; /* this is big[5][5] = 177; */

for (i = 0;i < 8;i++) {
                 for (j = 0;j < 8;j++)
                             printf("%5d ",big[i][j]);
                                printf("\n"); /* newline for each increase in i */
     }
}

This programme shows no errors and works perfectly but i cannot understand the mechanism behind that plz help me...
Its urjent....
plz,
Tell me the entire programme function...
Thnx....

above programme is right,

but you have to maintain the indentation in the programme.

above programme is right,

but you have to maintain the indentation in the programme.

After you posted that unformatted code in another thread you have the gall to mention indentation here? Practice what you preach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.