작성
·
377
0
구조체 선언할 때, dot을 사용해서 선언하면 아래와 같이 오류가 납니다!
주석된 부분에서 오류가 나타나는 이유를 알고 싶습니다!!
--------------------------------------------------------------------------------------------------
#define CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define LEN 41
struct names {
char given[LEN];
char family[LEN];
};
struct reservation {
struct names guest;
struct names host;
char food[LEN];
char place[LEN];
int year;
int month;
int day;
int hours;
int minutes;
};
int main(void)
{
//struct reservation res {
// .guest = { "Nick", "Carraway" },
// .host = { "Jay", "Gatsby" },
// .place = { "the Gatsby mansion" },
// .food = { "Escargot" },
// .year = 2022,
// .month = 4,
// .day=10,
// .hours = 18,
// .minutes=30
//};
struct reservation res {
{ "Nick", "Carraway" },
{ "Jay", "Gatsby" },
{ "Escargot" },
{ "the Gatsby mansion" },
2022,
4,
10,
18,
30
};
printf("\
Dear %s %s \n\
I would like to serve you %s.\n\
Please visit the Gatsby mansion on %d/%d/%d at %d:%d.\n\
Sincerely, %s %s\n",
res.guest.given, res.guest.family, res.food, res.day, res.month, res.year, res.hours, res.minutes, res.host.given, res.host.family);
return 0;
}
답변 1
0
구조체 필드가 아직 정의되지 않았어서 그렇습니다.
아래 코드를 참고해보세요.
#include <stdio.h>
#define LEN 41
struct names {
char given[LEN];
char family[LEN];
};
struct reservation {
struct names guest;
struct names host;
char food[LEN];
char place[LEN];
int year;
int month;
int day;
int hours;
int minutes;
};
int main(void)
{
struct reservation res = {
.guest = { "Nick", "Carraway" },
.host = { "Jay", "Gatsby" },
.food = "Escargot",
.place = "the Gatsby mansion",
.year = 2022,
.month = 4,
.day = 10,
.hours = 18,
.minutes = 30
};
printf("\
Dear %s %s \n\
I would like to serve you %s.\n\
Please visit %s on %d/%d/%d at %d:%d.\n\
Sincerely, %s %s\n",
res.guest.given, res.guest.family, res.food, res.place, res.day, res.month, res.year, res.hours, res.minutes, res.host.given, res.host.family);
return 0;
}