retagged by
2,553 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$
retagged by

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.

 

Answer:

Related questions

4 votes
4 votes
4 answers
2
admin asked Mar 31, 2020
1,398 views
What is the meaning of following declaration?int(*p[7])();$p$ is pointer to function$p$ is pointer to such function which return type is array$p$ is array of pointer to f...
2 votes
2 votes
2 answers
3
admin asked Mar 30, 2020
1,029 views
Output of following program#include<stdio.h int main() { int i=5; printf("%d %d %d", i++,i++,i++); return 0; }$7\:6\:5$$5\:6\:7$$7\:7\:7$Compiler Dependent
3 votes
3 votes
1 answer
4
admin asked Mar 30, 2020
1,720 views
Output of following program? #include<stdio.h void dynamic(int s,...) { printf("%d",s); } int main() { dynamic(2,4,6,8); dynamic(3,6,9); return 0; }$2\:3$Compiler Error$4...