C | Pointer Basics | Question 17
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; } |
chevron_right
filter_none
(A) 90.500000
3
(B) 90.500000
12
(C) 10.000000
12
(D) 0.500000
3
Answer: (A)
Explanation: When we add a value x to a pointer p, the value of the resultant expression is p + x*sizeof(*p) where sizeof(*p) means size of data type pointed by p. That is why ptr2 is incremented to point to arr[3] in the above code. Same rule applies for subtraction. Note that only integral values can be added or subtracted from a pointer. We can also subtract or compare two pointers of same type.
Quiz of this Question
Recommended Posts:
- C | Pointer Basics | Question 7
- C | Pointer Basics | Question 6
- C | Pointer Basics | Question 17
- C | Pointer Basics | Question 4
- C | Pointer Basics | Question 2
- C | Pointer Basics | Question 1
- C | Pointer Basics | Question 3
- C | Pointer Basics | Question 16
- C | Pointer Basics | Question 15
- C | Pointer Basics | Question 8
- C | Pointer Basics | Question 12
- C | Pointer Basics | Question 14
- C | Pointer Basics | Question 17
- C | Pointer Basics | Question 13
- C | Pointer Basics | Question 10



