C | Advanced Pointer | Question 9
#include <stdio.h> #include <stdlib.h> int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0; i<5; i++) *(ptr + i) = i; printf("%d ", *ptr++); printf("%d ", (*ptr)++); printf("%d ", *ptr); printf("%d ", *++ptr); printf("%d ", ++*ptr); } |
chevron_right
filter_none
(A) Compiler Error
(B) 0 1 2 2 3
(C) 0 1 2 3 4
(D) 1 2 3 4 5
Answer: (B)
Explanation: The important things to remember for handling such questions are
1) Prefix ++ and * operators have same precedence and right to left associativity.
2) Postfix ++ has higher precedence than the above two mentioned operators and associativity is from left to right.
We can apply the above two rules to guess all
*ptr++ is treated as *(ptr++)
*++ptr is treated as *(++ptr)
++*ptr is treated as ++(*ptr)
Quiz of this Question
Recommended Posts:
- C | Advanced Pointer | Question 3
- C | Advanced Pointer | Question 7
- C | Advanced Pointer | Question 8
- C | Advanced Pointer | Question 2
- C | Advanced Pointer | Question 1
- C | Advanced Pointer | Question 4
- C | Advanced Pointer | Question 10
- C | Advanced Pointer | Question 5
- Double Pointer (Pointer to Pointer) in C
- C++ | this pointer | Question 2
- C++ | this pointer | Question 5
- C++ | this pointer | Question 4
- C++ | this pointer | Question 3
- C++ | this pointer | Question 1
- C | Pointer Basics | Question 17



