2.6k views

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:

1. $0, 0$
2. $0, 1$
3. $1, 0$
4. $1, 1$

edited | 2.6k views
0
0

At first in loop we are giving $x=0$ then $ptr$ is pointing to $X$.

So, $*ptr=0$

Now, we copying the value of $ptr$ to $y$ ,so $Y=0$

 x=0;      //value of x = 0
ptr= &x;      // ptr points to variable x
y= *ptr;      // Y contain value pointed by ptr i.e. x= 0;

Now, value of $ptr$ is changed to $1$. so the location of $X$ itself got modified

 *ptr=1;

As it is pointing to $x$ so $x$ will also be changed to $1$

So, $1,0$ will be the value

by Boss (18k points)
edited
we got x= 1 and y= 1;
void printxy(int x, int y)
{
int *ptr;     //pointer is created which contain integer value.
x=0;          //value of x = 0 here.
ptr= &x;      // ptr point to variable which has 0
y= *ptr;      // y contain value pointed by ptr i.e. x= 0;
*ptr=1;       // value pointer by ptr is now set to 1 i.e. x= 1;
printf("%d,%d",x,y);  // print x,y = x= 1 y=0


}

by Veteran (62.7k points)

hope it might help.......

by Boss (41.9k points)

Here's the code with a full explanation.

void printxy(int x, int y) {
int *ptr;
x=0;     /*The value of x is now 0.*/

ptr=&x;  /*Assigning the address of x to ptr.
It means ptr points to the address of x.
So *ptr=0 as x = 0.
*/

y=*ptr;  /*Assigning the value that the ptr holds, to y
y=0 as *ptr=0.
*/

*ptr=1;  /*Assigning 1 to the value that the ptr holds.
It means x=*ptr=1 as ptr points to the address of x.
So the value of x is now 1.
*/

/*Therefore, x=1 and y=0 now */

printf(“%d, %d”, x, y);  /*It will give the output as 1, 0 */
}

Note that the function will print the same value no matter what the parameters are passed into it.

So the correct answer is C.

by Active (3.2k points)
+1 vote
C) 1,0
by Active (1.5k points)
+1 vote
Option C
by Active (2.7k points)
+1 vote
by Active (1.2k points)
+1 vote

Option c is right.

by Boss (35.7k points)