Skip to content
GATEiit

GATE CSE 2017 Session 2 Q24: Output of printxy(1,1) with pointer to a parameter

Syllabus topic: Programming in C

Question

Consider the following function implemented in C:

void printxy(int x, int y) {
      int *ptr;
      x = 0;
      ptr = &x;
      y = *ptr;
      *ptr = 1;
      printf("%d,%d",x,y);
}

The output of invoking printxy(1,1) is

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 1/3 mark in GATE.
Show answer and explanation

Answer: C (official GATE 2017 final answer key)

Concept used: Writing through a pointer changes the variable it points to, while a copy taken earlier keeps the old value.

Concept notes

Loading notes…

First step: Trace the values of x and y line by line, noting that y copies *ptr before *ptr is set to 1.

Full step-by-step solution

Loading…

Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.

More questions on Programming in C

Every PYQ on this topic →

Share this question

WhatsAppDownload image

Source: official GATE 2017 Session 2 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key