Ex21-자기 참조 구조체 예제

CODEDRAGON Development/C, C++

반응형

   

자기 참조 구조체 예제

   

   

메모리 구조

   

소스코드

   

#include <stdio.h>

struct student{
        char name[20];              //

        int age;                        //

        struct student* left_link;  //
자기 참조 구조체 포인터 변수
        struct student* right_link; //
자기 참조 구조체 포인터 변수
};

int main(void){
        struct student stu1 = {"Brenda", 20, NULL, NULL};
        struct student stu2 = {"Gina", 30, NULL, NULL};
        struct student stu3 = {"Zoe", 40, NULL, NULL};

        stu1.left_link = &stu2;
        stu1.right_link= &stu3;

        printf("%s, %d \n", stu1.name, stu1.age);
        printf("%s, %d \n", stu1.left_link->name, stu1.left_link->age);
        printf("%s, %d \n", stu1.right_link->name, stu1.right_link->age);

        return 0;
}


   

   

출력결과

   

   

   

   

   

반응형