GATE / CS ARCHIVE

GATE CSE 2008 / QUESTION 60

Aliased pointer updates and a value parameter

Programming in C · Pointers & Memory · Full GATE CSE 2008 paper

GATE 2008, Q60 2 marks · MCQ
Report issue

Pointers & Memory

Question 60, GATE 2008: What does this historical C program print?
int f(int x, int *py, int **ppz) {
 int y,z;
 **ppz += 1; z=*ppz;
 *py += 2; y=*py;
 x += 3;
 return x+y+z;
}
void main() {
 int c,*b,**a;
 c=4; b=&c; a=&b;
 printf("%d",f(c,b,a));
}

(A) 18
(B) 19
(C) 21
(D) 22
Tap to enlarge
Your answer
Answer & solution

Answer: B only if z=*ppz is repaired to z=**ppz; no portable numeric answer as printed (no official key was published)

1. The scan prints **ppz += 1; z = *ppz; with only one dereference in the assignment to z. Since ppz has type int **, *ppz is an int * rather than the integer value of c. Assigning that pointer to int z violates the standard C simple-assignment constraints and requires a diagnostic. Consequently the printed program has no portable numeric answer among the options; accepting it as an extension does not make z equal to 5.
2. If the apparent typo is explicitly repaired to z = **ppz, the intended alias trace is: x starts as a separate copy 4; **ppz += 1 makes c=5 and z=5; *py += 2 makes c=7 and y=7; x += 3 makes x=7. The sum is then 7+7+5=19, option B only for that repaired program.
3. The source also uses historical void main() and omits the stdio declaration. Those conventions do not remove the single-dereference defect. The derived answer is therefore conditional: B applies only after the stated repair, and no listed portable answer is justified for the printed code. No official key is available.

Original paper, page 12