in Programming retagged by
1,040 views
1 vote
1 vote

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 retagged by
1.0k views

1 comment

A is Answer...
0
0

1 Answer

3 votes
3 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.

 

2 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
Answer:

Related questions