Here is an initialization of a structure type. Tell what happens with each
initialization. Note any problems with these initializations.
struct Date
{
int month;
int day;
int year;
};
a. Date due_date = {12, 21};
b. Date due_date = {12, 21, 2022};
c. Date due_date = {12, 21, 20, 22};
d. Date due_date = {12, 21, 22};
Conisder the given data:
struct Date
{
int month;
int day;
int year;
};
a)
Date due_date = {12, 21};
Valid, but the year
member is uninitialized.
Explanation:
month
member of due_date
will be set to 12, the day
member will be set to 21, and the year
member will have an unspecified value.year
member will take whatever value happens to be in memory at the location reserved for the year
member of the structure.year
value will be uninitialized.b)
Date due_date = {12, 21, 2022};
Valid, all members are explicitly initialized.
Explanation:
due_date
are provided with values.month
member will be set to 12, the day
member will be set to 21, and the year
member will be set to 2022.c)
Date due_date = {12, 21, 20, 22};
Invalid, too many initializer values.
Explanation:
Date
structure only has three members.month
, day
, and year
members of the due_date
structure, respectively.d)
Date due_date = {12, 21, 22};
Valid, all members are explicitly initialized.
Explanation:
due_date
are provided with values.month
member will be set to 12, the day
member will be set to 21, and the year
member will be set to 22.