Consider the following C program. #include<stdio.h> int main() { static int…
2015
Consider the following C program.
#include<stdio.h> int main() { static int a[] = {10, 20, 30, 40, 50}; static int *p[] = {a, a+3, a+4, a+1, a+2}; int **ptr = p; ptr++; printf("%d%d", ptr-p, **ptr); }The output of the program is ______________.
Attempted by 105 students.
Show answer & explanation
Correct answer: 140
Key idea: pointer arithmetic on arrays gives element offsets, and double dereference retrieves the integer value.
p is an array of int pointers with values: p[0] = a, p[1] = a+3, p[2] = a+4, p[3] = a+1, p[4] = a+2.
ptr is set to p (points to p[0]). After ptr++, ptr points to p[1].
ptr - p evaluates to 1 because ptr now points to the second element of the array p.
*ptr is p[1] which is a+3, so **ptr is *(a+3) which equals a[3] = 40.
Output: printf("%d%d", ptr-p, **ptr) prints the two integers back to back, so the program prints 140.