Ex20-자기 참조 구조체

CODEDRAGON Development/C, C++

반응형

   

자기 참조 구조체

   

   

메모리 구조

초기화 후

   

link로 연결된 연결 리스트 구조

   

   

소스코드

   

#include <stdio.h>

struct student{
        char name[20];          //

        int age;                //

        struct student* link;   //
자기 참조 구조체 포인터 변수, 4byte
};
int main(void){
        struct student stu1 = {"Oscar", 27, NULL};
        struct student stu2 = {"Esther", 37, NULL};
        struct student stu3 = {"Sebastian", 47, NULL};

//      stu1.link = &stu2;
//      stu2.link = &stu3;
//      printf("%s %d \n", stu1.name, stu1.age);
//      printf("%s %d \n", stu1.link->name, stu1.link->age);
//      printf("%s %d \n", stu1.link->link->name, stu1.link->link->age);

        stu2.link = &stu1;
        stu3.link = &stu2;

        printf("%s, %d \n", stu3.name, stu3.age);
        printf("%s, %d \n", stu3.link->name, stu3.link->age);
        printf("%s, %d \n", stu3.link->link->name, stu3.link->link->age);

        return 0;
}


   

   

출력결과

   

반응형