Pointers
August 28, 2024Software-Engineering
August 28, 2024Pointers
|
Question 16
|
Assume that float takes 4 bytes, predict the output of following program.
#include
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;
}
#include
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;
}
|
90.500000 3
|
|
|
90.500000 12
|
|
|
10.000000 12
|
|
|
0.500000 3
|
Question 16 Explanation:
● float *ptr1=&arr[0]; // ptr1 will point to first element of the array.
● float *ptr2=ptr1+3; // ptr2 will point to fourth element of the array
● printf(“%f”,*ptr2); // It will display fourth element value which is 90.500000
● printf(“%d”,ptr2-ptr1); // Here both pointer will point to same array . The subtraction operation gives the number of elements between the addresses.
● float *ptr2=ptr1+3; // ptr2 will point to fourth element of the array
● printf(“%f”,*ptr2); // It will display fourth element value which is 90.500000
● printf(“%d”,ptr2-ptr1); // Here both pointer will point to same array . The subtraction operation gives the number of elements between the addresses.
Correct Answer: A
Question 16 Explanation:
● float *ptr1=&arr[0]; // ptr1 will point to first element of the array.
● float *ptr2=ptr1+3; // ptr2 will point to fourth element of the array
● printf(“%f”,*ptr2); // It will display fourth element value which is 90.500000
● printf(“%d”,ptr2-ptr1); // Here both pointer will point to same array . The subtraction operation gives the number of elements between the addresses.
● float *ptr2=ptr1+3; // ptr2 will point to fourth element of the array
● printf(“%f”,*ptr2); // It will display fourth element value which is 90.500000
● printf(“%d”,ptr2-ptr1); // Here both pointer will point to same array . The subtraction operation gives the number of elements between the addresses.
