A three dimensional array in 'C' is declared as int A[x][y][z]. Here, the…

2015

A three dimensional array in 'C' is declared as int A[x][y][z]. Here, the address of an item at the location A[p][q][r] can be computed as follows: (where w is the word length of an integer)

  1. A.

    &A[0][0][0]+w(y*z*q+z*p+r)

  2. B.

    &A[0][0][0]+w(y*z*p+z*q+r)

  3. C.

    &A[0][0][0]+w(x*y*p+z*q+r)

  4. D.

    &A[0][0][0]+w(x*y*q+z*p+r)

Attempted by 81 students.

Show answer & explanation

Correct answer: B

Key idea: C stores multidimensional arrays in row-major order, so the first index moves you by whole blocks of size y*z elements.

  • Step 1: Number of elements to skip for the first index p: p * (y * z).

  • Step 2: Number of elements to skip for the second index q within that block: q * z.

  • Step 3: Add the innermost index r.

Total element offset (in elements): p*y*z + q*z + r

Final address (in bytes): &A[0][0][0] + w*(p*y*z + q*z + r)

Note: p, q, r are assumed zero-based indices; w is the size in bytes of an int.

Explore the full course: Mppsc Assistant Professor