구조체를 사용하는 방법을 배우려고하지만 (아마도 매우 간단합니다) 문제가 있습니다. RECT 구조체에 속하는 POINT 구조체에 저장된 변수 x를 어떻게 참조 할 수 있습니까? 시작은 항상 저에게 어렵습니다. 간단히 r1.bottom_left
이 Point&
반환이 r1.bottom_left.x
다른 구조체의 일부인 구조체에 저장된 변수를 참조하는 방법
공지 사항 등의
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
struct Rect {
Point bottom_left, top_right;
};
void printPoint(const Point* p){
cout << "(" << p->x << "," << p->y << ")";
}
void printRect(const Rect* r){
cout << "["; printPoint(&r->top_right);
cout << ","; printPoint(&r->bottom_left);
cout << "]" << endl;
}
bool haveIntersection(const Rect* r1, const Rect* r2){
cout << &r1->bottom_left->x;
}
int main() {
Rect r1 = { {1,1}, {4,3} };
Rect r2 = { {2,0}, {3,4} };
Rect r3 = { {0,4}, {1,5} };
printRect(&r1);
haveIntersection(&r1, &r2);
}
대신 & R -> TOP_RIGHT의, 내가 선호 및 (R-> TOP_RIGHT) 연산자 우선 순위에 대한 혼란을 방지합니다. 또한, & r1-> bottom_left-> x가 올바르지 않습니다. –
@AbhishekBansal 나는이 & r1-> bottom_left-> x 문제를 가지고 있습니다. x 변수에 액세스하려면 어떻게해야합니까? – propict
(r1-> bottom_left) .x 또는 ((* r1) .bottom_left) .x –