📜  门| GATE-CS-2017(Set 2)|第35章

📅  最后修改于: 2021-06-29 03:25:02             🧑  作者: Mango

考虑以下在C中实现的函数:

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

printxy(1,1)的输出是
(A) 0,0
(B) 0,1
(C) 1,0
(D) 1,1答案: (C)
解释:

#include
void main()
{
 int x = 1, y = 1;
 printxy(x,y);
}
void printxy(int x, int y) 
{  
    int *ptr;     
    x = 0;     
    
    // ptr point to variable of value 0 
    ptr = &x;      
    
    // y has value pointed by ptr -> x= 0;
    y = *ptr;   
    
    // value pointed by ptr is set to 1 -> x= 1;
    *ptr = 1;       
    
    //x be changed to 1 and y will remain 0
    printf("%d,%d", x, y);  
}

这个问题的测验