Consider the following C program:
#include <stdio.h>
int main(){
int a;
int arr[5] = {30,50,10};
int *ptr;
ptr = &arr[0] + 1;
a = *ptr;
(*ptr)++;
ptr++;
printf("%d", a + (*ptr) + arr[1]);
return 0;
}The output of the above program is ___________. (Answer in integer)
Answer
111
Explanation
- The array holds {30, 50, 10, 0, 0}: elements without an initialiser become 0.
ptr = &arr[0] + 1points toarr[1], soa = 50.(*ptr)++increments the element, not the pointer, soarr[1]becomes 51. The copy inastays 50.ptr++moves the pointer toarr[2], so*ptris 10.
The program prints \(50 + 10 + 51 = 111\).
Answer: 111