22,312 views
62 votes
62 votes

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 _______.

11 Answers

Best answer
65 votes
65 votes
static int a[] = {10, 20, 30, 40, 50};
static int *p[] = {a, a+3, a+4, a+1, a+2};
int **ptr = p;

ptr++;

$\text{ptr-p} = \frac{\text{address of ptr} - \text{address of p}}{\text{sizeof(*ptr))}} = 1$

$\text{**ptr = p[2] = *(a+3) = 40}$

printf("%d%d", ptr-p, **ptr);  // 140
edited by
14 votes
14 votes
for ptr-p =1, as pointer increment adds the size of the data type and pointer subtraction gives the number of objects that can be held in between the two addresses = diff(addr1, addr2)/ sizeof(data type)

and for **ptr = *(a+3) = a[3] = 40
12 votes
12 votes

 

 

 

 

 

 

 

 

 

 

 

Correct answer will be $1$ and $40$ irrespective of size of pointer and data types

reshown by
Answer:

Related questions

47 votes
47 votes
8 answers
3
go_editor asked Feb 14, 2015
15,861 views
Consider the following C program segment.# include <stdio.h int main() { char s1[7] = "1234", *p; p = s1 + 2; *p = '0'; printf("%s", s1); }What will be printed by the pro...