구조체안의 구조체

ddong90의 이미지

구조체안의 구조체를 선언한후 안에 들어있는 구조체에 메모리를 할당하는방법

channel이라는 구조체에 user라는 구조체를 하나더 만들어

각 channel마다 user를 받아서 출력하는 것을 해보려는데요

연결리스트를 사용하는 초기설정에서...


typedef struct _user
{
        char user_name[20];
        struct _user *user_next;
}user;
 
typedef struct _channel
{
        int user_count;
        char channel_name[20];
        struct _channel *channel_next;
        user *user_head, *user_tail;
}channel;
channel *channel_head, *channel_tail;
 
void init_user(void)
{
        user_head=(user*)malloc(sizeof(user));
        user_tail=(user*)malloc(sizeof(user));
        user_head->user_next=user_tail;
        user_tail->user_next=user_tail;
}
//이부분에서 channel안의 u_head에 메모리를 할당하고 싶은데 이렇게가 아닌거 같아서요
 
void init_channel(void)
{
        channel_head=(channel*)malloc(sizeof(channel));
        channel_tail=(channel*)malloc(sizeof(channel));
        channel_head->channel_next=channel_tail;
        channel_tail->channel_next=channel_tail;
}
hd517의 이미지

channel_head 를 먼저 할당하셔서 user_head 포인터 공간을 확보하신다음

channel_head=(channel*)malloc(sizeof(channel));
channel_head.user_head=(user*)malloc(sizeof(user));

하셔야지요.