Programing - C/C Basic grammar

102 구조체를동적할당해서포인터로사용해보기

junmoyo 2017. 8. 7. 17:01

개념 :


동적할당이나 포인터로 참조할때는 접근 법이 닷에서 지시연산자로 바뀝니다.



목표 : 


예제101 소스를 정적할당에서 동적할당으로 변경하여 사용하기




소스코드 : main.c

#include <stdio.h>

#include <malloc.h>

struct  tagStudentScor

{

int kor;

int eng;

int math;


char szName[256];

};


int main(void)

{

struct tagStudentScor* pPage = NULL;

//메모리를 블럭으로 생성하고 초기화하는 함수호출 

pPage = calloc(sizeof(struct tagStudentScor), sizeof(struct tagStudentScor));


sprintf_s(pPage->szName, sizeof(pPage->szName), "LeeJunmo\0");

pPage->kor = 100;

pPage->eng = 100;

pPage->math = 100;


printf("성적카드 출력\n");

printf("이름 : %s \n", pPage->szName);

printf("국어 : %d \n", pPage->kor);

printf("영어 : %d \n", pPage->eng);

printf("수학 : %d \n", pPage->math);


free(pPage);


return 0;

}


결과



#구조체를동적할당해서포인터로사용해보기,#c언어, #c언어입문, #프로그램입문