다음은 구조체 배열에서 최댓값을 찾아 출력하는 C 프로그램이다. (가)… | [7급 국가직 자료구조론] 24년 1회차 기출문제 상세 페이지
1[7급 국가직 자료구조론] 24년 1회차
다음은 구조체 배열에서 최댓값을 찾아 출력하는 C 프로그램이다. (가), (나)에 들어갈 내용을 바르게 연결한 것은?#include <stdio.h>
typedef struct _data {
int value;
} DataType;
DataType *max(DataType *a) {
int i, max=a[0].value, index=0;
for(i=0; i<5; i++)
if(max<a[i].value) {
max=a[i].value;
index=i;
}
return ( (가) );
}
int main(void)
{
int i;
DataType data[5], *result;
for(i=0; i<5; i++)
scanf("%d", &data[i].value);
result=max(data);
printf("max=%d", ( (나) ));
return 0;
}
1
&a[index] / result->value
2
&a[index] / result.value
3
a[index] / result->value
4
a[index] / (*result).value
해설
(가)는 &a[index], (나)는 result->value이다.
max 함수의 반환형이 DataType *이므로 최댓값 원소의 주소를 반환해야 하고, 그 주소를 받은 result도 포인터이므로 멤버는 화살표 연산자로 접근한다.
- &a[index] / result.value 조합은 포인터인 result에 점(.) 연산자를 써서 컴파일 오류가 난다.
- a[index] / result->value 조합은 a[index]가 구조체 값 자체여서 포인터 반환형과 맞지 않는다.
- a[index] / (*result).value 조합은 (나)가 result->value와 같아 옳지만, 반환값 a[index]가 포인터가 아니다.
오류나 개정된 내용이 보이면 알려주세요