C | Pointer Basics | Question 3
Output of following program?
#include <stdio.h> int main() { int *ptr; int x; ptr = &x; *ptr = 0; printf ( " x = %d\n" , x); printf ( " *ptr = %d\n" , *ptr); *ptr += 5; printf ( " x = %d\n" , x); printf ( " *ptr = %d\n" , *ptr); (*ptr)++; printf ( " x = %d\n" , x); printf ( " *ptr = %d\n" , *ptr); return 0; } |
(A) x = 0
*ptr = 0
x = 5
*ptr = 5
x = 6
*ptr = 6
(B) x = garbage value
*ptr = 0
x = garbage value
*ptr = 5
x = garbage value
*ptr = 6
(C) x = 0
*ptr = 0
x = 5
*ptr = 5
x = garbage value
*ptr = garbage value
(D) x = 0
*ptr = 0
x = 0
*ptr = 0
x = 0
*ptr = 0
Answer: (A)
Explanation: See the comments below for explanation.
int *ptr; /* Note: the use of * here is not for dereferencing, it is for data type int */ int x; ptr = &x; /* ptr now points to x (or ptr is equal to address of x) */ *ptr = 0; /* set value ate ptr to 0 or set x to zero */ printf(" x = %d\n", x); /* prints x = 0 */ printf(" *ptr = %d\n", *ptr); /* prints *ptr = 0 */ *ptr += 5; /* increment the value at ptr by 5 */ printf(" x = %d\n", x); /* prints x = 5 */ printf(" *ptr = %d\n", *ptr); /* prints *ptr = 5 */ (*ptr)++; /* increment the value at ptr by 1 */ printf(" x = %d\n", x); /* prints x = 6 */ printf(" *ptr = %d\n", *ptr); /* prints *ptr = 6 */
Please Login to comment...