#include <stdio.h>
/* 구조체 정의
struct 구조체명 {
     자료형 변수명1;
     자료형 변수명2;
}; */

struct Student {
     char * name;
     int age;
     int number;
};

struct Friend {
     char * name;
     int age;
     struct Friend * best_f;
};


int main() {
     // 구조체 : 여러가지 자료를 한 번에 저장하는 자료형

     // 구조체 변수 만들기
     // struct 구조체명 변수명;
     struct Student s1;

     // 구조체 멤버 저장
     // 변수명.멤버명 = 값;
     s1.name = "주현";
     s1.age = 15;
     s1.number = 20301;

     printf("====학생 정보====\n");
     printf("이름 : %s\n", s1.name);
     printf("나이 : %d\n", s1.age);
     printf("학번 : %d\n", s1.number);

     struct Student s2 = {"승민", 16, 30312};
     printf("====학생 정보====\n");
     printf("이름 : %s\n", s2.name);
     printf("나이 : %d\n", s2.age);
     printf("학번 : %d\n", s2.number);

     struct Student s3 = {"승준", 16, 31132};
     printf("====학생 정보====\n");
     printf("이름 : %s\n", s3.name);
     printf("나이 : %d\n", s3.age);
     printf("학번 : %d\n", s3.number);

     // 구조체 배열
     struct Student ss[3] = {
          {"민지", 14, 10101},
          {"수현", 15, 20102},
          {"지우", 16, 30403}
     
     };
     printf("====학생 정보====\n");
     printf("이름 : %s\n", ss[1].name);
     printf("나이 : %d\n", ss[1].age);
     printf("학번 : %d\n", ss[1].number);

     // 구조체 포인터
     //friend2
     struct Friend friend2 = {"수현", 15, NULL};

     //friend1
     struct Friend friend1 = {"민지", 14, &friend2};

     printf("====친구1 카드====\n");
     printf("변수 주소 : %p\n", &friend1);
     printf("이름 : %s\n", friend1.name);
     printf("나이 : %d\n", friend1.age);
     printf("best_f : %p\n", friend1.best_f);

     printf("====친구2 카드====\n");
     printf("변수 주소 : %p\n", &friend2);
     printf("이름 : %s\n", (*friend1.best_f).name);
     printf("나이 : %d\n", (*friend1.best_f).age);
     printf("best_f : %p\n", (*friend1.best_f).best_f);

     printf("====친구2 카드====\n");
     printf("이름 : %s\n", friend1.best_f->name);
     printf("나이 : %d\n", friend1.best_f->age);
     printf("best_f : %p\n", friend1.best_f->best_f);


     return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: