StreakPeaked· Practice

ExamsGATETechnical

For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits. t0 = i * 1024 t1 = j * 32 t2 = k * 4 t3 = t1 + t0 t4 = t3 + t2 t5 = X[t4] Which one of the following statements about the source code for the C program is correct?

  1. X is declared as "int X[32][32][8]".
  2. X is declared as "int X[4][1024][32]".
  3. X is declared as "char X[4][32][8]".
  4. X is declared as "char X[32][16][2]".

Correct answer: X is declared as "int X[32][32][8]".

Solution

In row-major order, the offset for X[i][j][k] in a 3D array int X[a][b][c] is ((i·b + j)·c + k)·4 bytes. The given code computes i·1024 + j·32 + k·4, which matches ((i·32 + j)·8 + k)·4. Hence the declaration is int X[32][32][8].

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →