recategorized by
9,550 views
30 votes
30 votes

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$
recategorized by

11 Answers

Best answer
37 votes
37 votes

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

C is correct answer here.

edited by
8 votes
8 votes
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

}

C is answer

Answer:

Related questions

70 votes
70 votes
13 answers
1
Madhav asked Feb 14, 2017
27,483 views
Consider the following C program.#include<stdio.h #include<string.h int main() { char* c="GATECSIT2017"; char* p=c; printf("%d", (int)strlen(c+2[p]-6[p]-1)); return 0; }T...
25 votes
25 votes
7 answers
3
Madhav asked Feb 14, 2017
11,774 views
Consider the following C program.#include<stdio.h int main () { int m=10; int n, n1; n=++m; n1=m++; n ; n1; n-=n1; printf(“%d”, n); return 0; }The output of the prog...
26 votes
26 votes
3 answers
4
khushtak asked Feb 14, 2017
5,770 views
Match the following:$$\begin{array}{|ll|ll|}\hline P. & \text{static char var ;} & \text{i.} & \text{Sequence of memory locations to store addresses} \\\hline Q. & \text...