in Programming in C retagged by
2,547 views
2 votes
2 votes

Assume that float takes $4$ bytes, predict the output of following program.

#include<stdio.h>
int main() 
{ 
float arr[5]={12.5,10.0,13.5,90.5,0.5}; 
float *ptr1=&arr[0]; 
float *ptr2=ptr1+3; 
printf("%f",*ptr2); 
printf("%d",ptr2-ptr1); 
return 0; 
} 
  1. $90.500000\:\:3$
  2. $90.500000\:\:12$
  3. $10.000000\:\:12$
  4. $0.500000\:\:3$
in Programming in C retagged by
by
2.5k views

1 comment

A is Answer...
0
0

1 Answer

4 votes
4 votes
float arr[5]={12.5,10.0,13.5,90.5,0.5};
float *ptr1=&arr[0]; // point to first element of arr
float *ptr2=ptr1+3; // point to fourth element of arr
printf("%f",*ptr2); // prints 90.500000
printf("%d",ptr2-ptr1); // pointer substraction is scaled to base type, gives number of elements between the two. prints 3

So A is correct.

 

3 Comments

Good explain
0
0
no of elements is 2 between p1 and p2. how can the count be 3, is p1 also counted?
0
0
have you got the asnwer?Please explain how the count comes 3
0
0
Answer:

Related questions