본문 바로가기
프로그램/C언어 1000제

C언어 125제] C언어 콘서트 CHAPTER 13 Programming 5 구조체를 동적메모리 할당으로 생성하는 프로그램 작성하기. p511

by 건티 2022. 2. 8.
728x90

출처 : 반크_세계유산 석굴암

 

참고풀이]

#include <stdio.h>
#include <stdlib.h> //malloc(), free(), exit()

typedef struct rec{
   int i;
   float PI;
   char A;
} my_record;

int main()
{
   //동적메모리를 할당한다.
   my_record *M=(my_record *)malloc(sizeof(my_record)*1);

   if(M==NULL)
   {
      printf("메모리 할당 오류\n");
      exit(1);
   }

   //할당된 동적메모리에 값을 넣는다.
   M[0].i=10;
   M[0].PI=3.140000;
   M[0].A='a' ;

   //결과출력
   printf("%d\n",M[0].i);
   printf("%f\n",M[0].PI);
   printf("%c\n",M[0].A);

   //할당한 동적메모리 해제
   free(M);

   return 0;
}

 

참고풀이 결과]

 

 

 

대한민국의 아름다운 영토, 독도의 가을

 

반응형

댓글