두 구조체의 데이터 멤버로 상대방 구조체 변수를 지정 할 수 있나요?

bowme의 이미지

struct brother {
    char name[20];
    int age;
    sister sib;
} a;
 
struct sister {
    char name[20];
    int age;
    brother sib;
} b;

그냥 이렇게 하니까 brother 구조체에서 sister라는 자료형이 없다고 에러가 뜨던데 두 구조체를 각각 상대방의 멤버로 만들려면 어떻게 해야 하나요?

dg의 이미지

struct sister;
struct brother {
char name[20];
int age;
sister sib;
} a;
 
struct sister {
char name[20];
int age;
brother sib;
} b;

하면...... 안됩니다... ㅡㅡ;;
brother가 sister를 포함하고 그 sister가 다시 brother를 포함하고..
이런식으로 구조체의 크기가 무한하게 되죠..
따라서
struct sister;
struct brother {
char name[20];
int age;
sister *sib;
} a;
 
struct sister {
char name[20];
int age;
brother *sib;
} b;

이런식으로 해서 new로 할당하면 됩니다..
bowme의 이미지

답변 감사합니다.
그런데 new로 할당하는건 어떤 차이가 있는지 알 수 있을까요?

bus710의 이미지


search : linked list

sorry, i can't type korean^^;;

life is only one time

익명사용자의 이미지

답은 위에 처럼 포인터를 쓰시면 됩니다.

근데 brother 구조체가 sister 를 가질 필요가 있나요?

sibiling 을 밖에서 표현하는게 자연스러워 보입니다.