search
Log In
18 votes
3.3k 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$
in Programming
edited by
3.3k views
0
already solved
0
Answer will be (c). x=1,y=0

12 Answers

30 votes
 
Best answer

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
7 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

5 votes

hope it might help.......

3 votes

Option c is right.

2 votes

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.

1 vote
C) 1,0
1 vote
Option C
1 vote
C is the answer
Answer:

Related questions

16 votes
5 answers
1
2.5k views
Match the following: ... P-ii; Q-iv; R-i; S-iii P-ii; Q-i; R-iv; S-iii P-ii; Q-iv; R-iii; S-i P-iii; Q-iv; R-i; S-ii
asked Feb 14, 2017 in Programming khushtak 2.5k views
37 votes
7 answers
2
6.7k views
Consider the following snippet of a C program. Assume that swap $(\&x, \&y)$ exchanges the content of $x$ and $y$: int main () { int array[] = {3, 5, 1, 4, 6, 2}; int done =0; int i; while (done==0) { done =1; for (i=0; i<=4; i++) { if (array[i] < array[i+1] ... if (array[i] > array[i-1]) { swap(&array[i], &array[i-1]); done =0; } } } printf( %d , array[3]); } The output of the program is _______
asked Feb 14, 2017 in Programming Arjun 6.7k views
25 votes
6 answers
3
4.9k views
Consider the C program fragment below which is meant to divide $x$ by $y$ using repeated subtractions. The variables $x$, $y$, $q$ and $r$ are all unsigned int. while (r >= y) { r=r-y; q=q+1; } Which of the following conditions on the variables $x, y, q$ and $r$ before the execution of the fragment will ensure that ... $(q==0) \ \&\& \ (r==x) \ \&\& \ (y >0)$ $(q==0) \ \&\& \ (y>0)$
asked Feb 14, 2017 in Programming Arjun 4.9k views
42 votes
9 answers
4
10.9k 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; } The output of the program is _______
asked Feb 14, 2017 in Programming Madhav 10.9k views
...