C 언어 자기 참조 구조체

#include <stdio.h> #include <stdlib.h> struct linked_list {         char name[20];         int age;         struct linked_list *left_link;         struct linked_list *right_link; }; int main() {         struct linked_list first_person =  {"남동길", 28, NULL, NULL};         struct linked_list second_person = {"아무개", 30, NULL, NULL};         struct linked_list third_person = {"박서방", 35, NULL, NULL};         first_person.right_link = &second_person;         second_person.left_link = &first_person;         second_person.right_link = &third_person;         third_person.left_link = &second_person;         printf("첫번째 사람 : %s\t%d\n",first_person.name, first_person.age);         prin...

C 언어 구조체 배열의 동적 할당

포인터변수를 선언하고 malloc()함수를 사용해서 메모리를 할당하고 free()함수로 해제 #include <stdio.h> #include <stdlib.h> struct s_teacher {         char name[20];         int age;         long number; }; struct s_student {         char name[20];         int age;         long number;         struct s_teacher* pTeacher; //포인터 변수 }; void inputCounts(int *sCnt, int *tCnt); int main() {         int sCnt =0, tCnt =0;         inputCounts(&sCnt, &tCnt);         if(sCnt <= 0 || tCnt <= 0)         {                 printf("선생과 학생수는 1명 이상이여야 됩니다.\n");                 return 0;         }         printf("선생 수 : %d, \t 학생 수  : %d\n", ...

C 언어 구조체의 포인터 멤버 변수

학생11, 학생21은 선생1을 담임선생으로, 학생21, 학생22는 선생2를 담임선생으로 두고 있는 코드이다. #include <stdio.h> struct s_teacher { char name[20]; int age; long number; }; struct s_student { char name[20]; int age; long number; struct s_teacher* pTeacher; //포인터 변수 }; int main() { struct s_student student11 = {"Lee", 20, 4567}; struct s_student student12 = {"Park", 21, 4568}; struct s_student student21 = {"Choi", 20, 4567}; struct s_student student22 = {"Han", 21, 4568}; struct s_teacher teacher1 = {"Kim", 35, 1234}; struct s_teacher teacher2 = {"Nam", 28, 1235}; student11.pTeacher = &teacher1; student12.pTeacher = &teacher1; student21.pTeacher = &teacher2; student22.pTeacher = &teacher2; printf("포인터를 이용한 구조체 변수 접근\n"); printf("학생 %s의 담임교사 이름 : %s\n", student11.name,student11.pTeacher->name); printf("학생 %s의 담임교사 교번 : %ld\n",student12.name,student...

C 언어 중첩구조체 활용과 구조체 포인터를 사용한 직사각형 좌표 출력

s_rectangle 구조체는 중첩구조체로 left_top이라는 s_point 구조체 멤버변수를 가진다. 직사각형의 너비와 너비를 입력받아 나머지 right_top, left_bottom, right_bottom 좌표를 출력해주는 코드이다. #include <stdio.h> struct s_point {         int x;         int y; }; struct s_rectangle {         struct s_point left_top;         int width; //너비         int height; //높이 }; void print_rectangle_info(struct s_rectangle rectangle); void input_rectangle_info(struct s_rectangle *pRectangle); int main() {         struct s_rectangle rectangle = {0};         input_rectangle_info(&rectangle);         print_rectangle_info(rectangle);         return 0; } void input_rectangle_info(struct s_rectangle *pRectangle) {         if(NULL != pRectangle)         {              ...

C 언어 중첩 구조체 선언과 초기화

#include <stdio.h> struct s_teacher { long number; char name[20]; int age; }; struct s_student { sturct s_teacher nested_teachar; long number; char name[20]; int age; int score; }; void print_student_info(struct s_student student); int main() { struct s_student student1 = {{1234,"Kim", 35}, 4567, "Lee", 20, 30 }; struct s_student student2 = {{1234,"Choi"}, 4568, "Lee", 20, 30 }; // 선생 age는 자동으로 0으로 초기화 struct s_student student3 = {1234,"Kim", 35, 4569, "Bak", 21 }; //학생 score는 자동으로 0 으로 초기화 print_student_info(student1); print_student_info(student2); print_student_info(student3); return 0; } void_student_info(struct s_student student) { printf("이름 : %s\t 나이 : %d\t 학번 : %ld\t 점수 : %d\n", student.name, student.age, student.number, student.score); printf("담임선생 이름 : %s\t 담임선생 나이 : %d\t 담임선생 번호 : %ld\n",student.nested_teacher.name, student.nested.age,...

C 언어 구조체 배열 선언과 초기화 방법들

1. 선언과 동시에 0으로 초기화 2. memset() 함수를 이용한 초기화 3. 초기화 목록을 이용한 초기화 4. 반복문을 이용한 초기화 #include <stdio.h> #include <string.h> struct student {         char name[20];         int age;         long number; }; int main() {         //1. 선언과 동시에 0으로 초기화         struct student students[3] = {0};         //2. memset() 함수를 이용한 초기화         struct student students2[3];         memset(students2,0,sizeof(students2));         //3. 초기화 목록을 이용한 초기화         struct student students3[3] = {                 {"kim", 25, 45607},                 {"기리",28,60121315},                 {"Lee",23, 45609}         };         //4. 반...

C 언어 1차원배열 2차원 배열을 활용한 학생관리

학생 수를 입력받아서 국어점수 배열, 영어점수 배열, 수학점수 배열, 총점수 배열과, 학생이름을 담을 배열들을 동적으로 할당한다. 그리고 각각의 점수와 이름들을 입력받고, 각각의 총점과 평균점수를 출력해주고 할당된 메모리를 해제해주는 코드이다. #include <stdio.h> #include <stdlib.h> #include <string.h> int get_student_num(); int alloc_students_scores(int num_student, int** ppscore_kor, int** ppscore_eng, int** ppscore_math                                 , int** ppscore_total); char** alloc_students_name(int num_student); void input_students_info(int num_student, int* pscore_kor, int* pscore_eng, int* pscore_math,                         int* pscore_total,  char **ppstudent_name); void print_avg(int num_student, int* pscore_total, char** ppstudent_name); void print_avgPerSub_totalAvg(int num_student,int* pscore_kor, int* pscore_eng, int* pscore_math, int* pscore_total); void freeMemory(int num_student,int* pscore_ko...