Look at the following code. structure Rectangle { int length; int width; }; Rectangle *r; Write statements that A) Dynamically allocate a Rectangle structure variable and use r to point to it. B) Assign 10 to the structure’s length member and 14 to the structure’s width member.

Short Answer

Expert verified
Answer: First, dynamically allocate a Rectangle structure variable using the "new" keyword and have "r" point to it. Second, use the pointer operator (->) to assign the values 10 and 14 to the attributes "length" and "width" respectively.

Step by step solution

01

Dynamically allocate a Rectangle structure variable

To allocate a dynamic Rectangle structure variable, use the "new" keyword. The new keyword will allocate memory for a Rectangle structure variable in the heap, and return a pointer to the newly created instance. ```cpp r = new Rectangle; ```
02

Assign values to the structure's length and width members

Use the pointer operator (->) to assign values to the length and width members of the structure pointed to by "r". ```cpp r->length = 10; r->width = 14; ```

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free