Pointers & Memory
![Question 36, GATE 2024: C code declares double a[2]={20.0,25.0}; sets p=a and q=p+1; and prints "%d,%d" with (int)(q−p), (int)(*q−*p). Which output? A 4,8; B 1,5; C 8,5; D 1,8.](../../assets/questions/CS-2024-S2-Q36-1.webp)
Answer & solution
Official answer: B
Approach: Evaluate the pointer expression and the dereferenced arithmetic expression separately.
1. Derivation. p points to a[0] and q=p+1 points to a[1]. Pointer subtraction counts array elements, not bytes, so q−p=1. The values differ by 25.0−20.0=5.0, cast to 5. The output is 1, 5, option B, independent of sizeof(double).
2. Explanation and checks. Adding one to a double pointer moves by the storage occupied by one double, but subtracting two double pointers converts the byte displacement back into an element count. Thus it is one regardless of the implementation’s double size. Dereferencing yields the numeric values 25.0 and 20.0, whose difference is exactly 5.0 here. The integer casts produce 1 and 5 for the two %d arguments; they do not turn the pointer difference into a byte distance.
The printed program uses typographic quotation marks and en dashes. Interpreting these as the intended ASCII C quotation marks and subtraction operators gives the stated answer B; copied literally, the printed characters do not form valid C tokens.