单选题 适中
#19844

运行如下代码会输出( )。 struct Point { int x , y ; } ; struct Rectangle { Point topLeft ;…

运行如下代码会输出( )。

struct Point { 
    int x, y; 
}; 
struct Rectangle { 
    Point topLeft; 
    Point bottomRight; 
}; 
int main() { 
    Rectangle rect = {{10, 10}, {20, 20}}; 
    rect.topLeft.x = 5; 
    Point* p = &rect.bottomRight; 
    p->y = 5; 
    cout << rect.topLeft.x + rect.bottomRight.y; 
    return 0; 
}
未登录也能判分,登录后才会记录
答案与解析
正确答案:A
解析
首先定义结构体Point包含两个整型成员x和y,结构体Rectangle包含两个Point类型的成员topLeft和bottomRight。在main函数中,声明Rectangle类型的变量rect并用嵌套初始化列表赋初值:topLeft的x和y被初始化为10,bottomRight的x和y被初始化为20。接着将rect.topLeft.x的值修改为5。然后定义指向Point类型的指针p,并将其指向rect.bottomRight的地址。通过指针p访问其指向对象的y成员,并将其值修改为5。最后计算rect.topLeft.x与rect.bottomRight.y的和。