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)
View worked solution
Answer: 111:111
Let us trace the execution of the program step-by-step:
- Array Initialization:
int arr[5] = {30, 50, 10};initializes the array elements as:arr[0] = 30,arr[1] = 50,arr[2] = 10,arr[3] = 0, andarr[4] = 0. - Pointer Assignment:
ptr = &arr[0] + 1;setsptrto point toarr[1]. - Value Assignment:
a = *ptr;assigns the value ofarr[1]toa. Thus,a = 50. - Increment Value:
(*ptr)++;increments the value pointed to byptr(which isarr[1]).
So,arr[1]becomes51. - Increment Pointer:
ptr++;advancesptrto point to the next element,arr[2]. - Print Statement:
The expression evaluated isa + (*ptr) + arr[1].
Substituting the values:
Thus, the output of the program is 111.
Free practice · Your answers on this page are not saved.
