For a C program accessing , the following intermediate code is generated by a compiler. Assume that the size of an integer is bits and the size of a character is 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?
View worked solution
Answer: A
In C, multi-dimensional arrays are stored in row-major order. For a 3D array declared as type X[D1][D2][D3], the byte offset of the element from the base address is calculated as:
where is the size (in bytes) of each element of the array.
Let us compare this formula with the given intermediate code:
t0 = i * 1024t1 = j * 32t2 = k * 4
Step 1: Determine the data type
Since bytes, and the question specifies that an integer is bits ( bytes) and a character is bits ( byte), the array must be of type int. This eliminates options C and D.
Step 2: Determine the dimensions
- Using and :
- Using :
Thus, the array must be declared with dimensions and , which matches int X[D1][32][8].
Looking at the options, Option A (int X[32][32][8]) is the only correct match.
Free practice · Your answers on this page are not saved.
