GATE | GATE-CS-2015 (Set 3) | Question 65
Consider the following C program.
The output of the program is _________
In order to simplify programs involving complex operations on pointers, we suggest you to draw proper diagrams in order to avoid silly mistakes. Let’s assume that integer is of 4 Bytes and Pointer size is also 4 Bytes.
Let’s assume array a Base address is 1000. Array name actually holds the array base address.
Let’s assume array p Base address is 2000.
Double Pointer ptr Base Address is 3000.
Now ptr is actually pointing to the first element of array p. ptr++ will make it point to the next element of array p. Its value will then change to 2004.
One of the Rule of Pointer Arithmetic is that When you subtract two pointers, as long as they point into the same array, the result is the number of elements separating them.
ptr is pointing to the second element and p is pointing to the first element so ptr-p will be equal to 1(Excluding the element to which ptr is pointing).
Now ptr = 2004 —–> *(2004) = 1012 —-> *(1012) —-> 40.
Therefore, final answer is 140.
This solution is contributed by Pranjul Ahuja.
Please Login to comment...