다음은 동적 메모리 할당을 통해 구조체 공간을 확보하고 이름과 학번을 입력받아 구조체에 저장하는 C 언어 함수이다. ㉠, ㉡에 들어갈 내용으로 옳게 짝지어진 것은?
#include <stdio.h>
#include <stdlib.h>
struct student{
char name[20];
int id;
};
struct student *alloc()
{
struct student *p;
p = ㉠ ;
if(p==NULL){
printf("Memory allocation error.");
return p;
}
printf("Student name?\n");
gets(p->name);
printf("Student id?\n");
scanf("%d", ㉡ );
return p;
} ㉠ ㉡
- ①(struct student)malloc(sizeof(struct student)) &((*p).id)
- ②(struct student *)malloc(sizeof(struct student)) p->id
- ③(struct student)malloc(sizeof(struct student)) (*p).id
- ④(struct student *)malloc(sizeof(struct student)) &(p->id)
선지를 골라 바로 채점해 보세요.
정답 ④
출처: 인사혁신처 공개 기출(공공데이터포털, 이용허락 제한 없음)