What will be output of the following program? #include<stdio.h> int main(){…
What will be output of the following program?
#include<stdio.h>
int main(){
int a=11,b=22,c=33;
int * arr[5]={&a,&b,&c};
printf("%d ",*(*arr+1));
return 0;
}
Answer: C. 22 — Answer: 22 (prints 22 on typical compilers). arr is an array of integer pointers: arr[0] = &a (11), arr[1] = &b (22), arr[2] = &c (33). Evaluate *(*arr + 1):…
- A.
Compilation Error
- B.
11
- C.
22
- D.
33
Attempted by 298 students.
Show answer & explanation
Correct answer: C
Answer: 22 (prints 22 on typical compilers).
arr is an array of integer pointers: arr[0] = &a (11), arr[1] = &b (22), arr[2] = &c (33).
Evaluate *(*arr + 1):
Step 1: *arr is arr[0], which is &a.
Step 2: (*arr) + 1 performs pointer arithmetic on an int*: it yields the address one int after a (i.e., &a + 1).
Step 3: Dereferencing that address reads the integer stored there. On typical compilers the local variables a, b, c are placed consecutively, so that address holds b (22).
Important note: Adding 1 to &a and dereferencing it relies on the variables being adjacent in memory and therefore is not guaranteed by the C standard; dereferencing (&a + 1) is undefined behavior in strictly conforming code. The MCQ expects 22 based on typical memory layout, but this is not portable.
Also note the distinction between the expressions: (*arr) + 1 is different from *(arr + 1). Here the code uses (*arr) + 1, not (arr + 1).