📜  门| GATE-CS-2009 |问题12(1)

📅  最后修改于: 2023-12-03 15:42:17.090000             🧑  作者: Mango

GATE-CS-2009 Problem 12

Problem Statement

Consider the following C program fragment:

int main() {
    int i, *ptr;
    i = 0;
    ptr = &i;
    i++;
    (*ptr)++;
    printf("%d %d", i, *ptr);
    return 0;
}

What is the output of the program?

(A) 0 0
(B) 0 1
(C) 1 0
(D) 1 1

Solution

The program declares an integer variable i and a pointer variable ptr that points to an integer. The program initializes i to 0 and sets the value of ptr to the address of i. The program then increments i and the value pointed to by ptr. Finally, the program prints the values of i and *ptr (i.e., the value pointed to by ptr).

The output of the program will be:

1 1

Therefore, the correct answer is option (D).

Explanation: i is incremented to 1 and *ptr is also incremented to 1 because ptr points to i. Thus, the values of i and *ptr are both 1 when printed.

The program declares an integer variable `i` and a pointer variable `ptr` that points to an integer. The program initializes `i` to 0 and sets the value of `ptr` to the address of `i`. The program then increments `i` and the value pointed to by `ptr`. Finally, the program prints the values of `i` and `*ptr` (i.e., the value pointed to by `ptr`).

The output of the program will be:

1 1


Therefore, the correct answer is option (D).

Explanation: `i` is incremented to 1 and `*ptr` is also incremented to 1 because `ptr` points to `i`. Thus, the values of `i` and `*ptr` are both 1 when printed.