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,student12.pTeacher->number);
printf("학생 %s의 담임교사 이름 : %s\n", student21.name,student21.pTeacher->name);
printf("학생 %s의 담임교사 교번 : %ld\n",student22.name,student22.pTeacher->number);

return 0;
}


실행 시 :
ndgndg91@LAPTOP-CCFK7MKV:~$ ./pointerMemberV
포인터를 이용한 구조체 변수 접근
학생 Lee의 담임교사 이름 : Kim
학생 Park의 담임교사 교번 : 1234
학생 Choi의 담임교사 이름 : Nam
학생 Han의 담임교사 교번 : 1235

댓글

이 블로그의 인기 게시물

About Kafka Basic

About JVM Warm up

Spring Boot Actuator readiness, liveness probes on k8s

About idempotent

About ZGC

About G1 GC

sneak peek jitpack

HackerRank Java Between Two Sets

Optimistic Concurrency Control VS Pessimistic Concurrency Control - What should i choose?

I need to know a little JVM