147k views
0 votes
Look at the following structure declaration: struct Container { int length; int width; int height; }; Container *dimension = nullptr; Write statements that (A) dynamically allocate a Container structure variable and use dimension to point to it. (B) Use C++'s structure pointer operator to assign 100 to the structure’s length member, 140 to the structure’s width member and 80 to the structure’s height member.

1 Answer

4 votes

Final answer:

To dynamically allocate a Container structure and assign values to its members, use 'new' for allocation and the '->' operator for accessing members.

Step-by-step explanation:

The objective of the question is to work with structures and dynamic memory allocation in C++. The following statements achieve the two tasks specified:

  1. Dynamically allocate a Container structure:

dimension = new Container;

  1. Assign values to the members of the structure using the pointer:

dimension->length = 100;

dimension->width = 140;

dimension->height = 80;

Here, the new keyword is used to allocate memory on the heap for a Container structure, and this memory is assigned to the pointer dimension. Then, the structure pointer operator (->) is used to access and set values for length, width, and height members of the structure.

User Babtek
by
7.0k points