SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Defining Classes
Exercise:
Self-test Exercises
Question:5 | ISBN:9780321531346 | Edition: 7

Question

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};

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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:

  • In this initialization, the 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.
  • This is because only the first two members (month and day) are explicitly provided in the initializer.
  • The year member will take whatever value happens to be in memory at the location reserved for the year member of the structure.
  • This can lead to unexpected behavior, as the year value will be uninitialized.

b)

Date due_date = {12, 21, 2022};

Valid, all members are explicitly initialized.

Explanation:

  • In this initialization, all three members of due_date are provided with values.
  • The month member will be set to 12, the day member will be set to 21, and the year member will be set to 2022.
  • This initialization is valid and sets all members of the structure to specific values.

c)

Date due_date = {12, 21, 20, 22};

Invalid, too many initializer values.

Explanation:

  • In this initialization, four values are provided in the initializer, but the Date structure only has three members.
  • The first three values (12, 21, and 20) will be assigned to the month, day, and year members of the due_date structure, respectively.
  • The fourth value (22) will have no place to go, and this initialization will likely result in a compilation error because there are too many initializer values.

d)

Date due_date = {12, 21, 22};

Valid, all members are explicitly initialized.

Explanation:

  • In this initialization, all three members of due_date are provided with values.
  • The month member will be set to 12, the day member will be set to 21, and the year member will be set to 22.
  • This initialization is valid and sets all members of the structure to specific values.

 

0 0

Discussions

Post the discussion to improve the above solution.