All Exams Test series for 1 year @ ₹349 only
Question

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)

Let's analyze the given C program step by step:

  1. int arr[5] = {30,50,10};
    This initializes an integer array arr with the first three values as 30, 50, and 10. Remaining elements are initialized to 0.
  2. int *ptr;
    This declares a pointer ptr of type integer.
  3. ptr = & arr[0] + 1;
    This sets ptr to point to arr[1]. Initially, arr[0] is 30, and arr[1] is 50, so ptr now points to the value 50.
  4. a = *ptr;
    This assigns the value pointed to by ptr (50) to a, so a = 50.
  5. (*ptr)++;
    This increments the value at ptr (which is arr[1]) by 1, changing the value from 50 to 51.
  6. ptr++;
    This increments ptr so that it now points to arr[2], which is initially 10.
  7. printf("%d", a + (*ptr) + arr[1]);
    We need to evaluate the expression a + (*ptr) + arr[1]:
    • a is 50.
    • *ptr now points to arr[2], which is 10.
    • arr[1] was incremented to 51.

The final expression becomes 50 + 10 + 51 = 111.

The computed result, 111, fits within the specified range (111, 111).

Therefore, the output of the program is 111.

Was this answer helpful?

Important Questions from Pointer

  1. What would be the equivalent pointer expression for referring the array element ar[m][n][o]
  2. Only legal pointer operations:
    A. pointer + number $\rightarrow$ pointer
    B. pointer - number $\rightarrow$ number
    C. pointer + pointer $\rightarrow$ pointer
    D. pointer - pointer $\rightarrow$ pointer
    E. pointer - pointer $\rightarrow$ number
    Choose the most appropriate answer from the options given below:
  3. Consider the following C program:
    #include <stdio.h>
    void stringcopy(char *, char *);
    int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
    }
    void stringcopy(char *s, char *t) {
    while(*t)
    *s++ = *t++;
    }
    Which ONE of the following will be the output of the program?

  4. Consider the following ANSI-C program.

    #include <stdio.h>
    int main(){
     int *ptr, a, b, c;
     a=5; b=11; c=20;
     ptr=&a; *ptr=c; ptr=&c;
     a=*(&b); c=*ptr-a;
     printf("%d",c);
     return(0);
    }

    The output of this program is ____________. (answer in integer)

    Note: Assume that the program compiles and runs successfully.

  5. The following statement in ‘C’
    int (*f())[ ];
    declares
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App