data-structures · beginner · ~15 min
Pass a struct by value and use its fields.
Compute the area of a rectangle from its width and height.
Define struct Rect { int w, h; }; and implement long rect_area(struct Rect r) that returns the area w * h.
One struct Rect value passed by value, with int fields w (width) and h (height).
The area w * h as a long.
r={3,4} -> 12
r={10,10} -> 100
r={0,5} -> 0
long before multiplying so large dimensions do not overflow.One struct Rect value (with int w, h), passed by value.
long: the area w * h.
Cast to long before multiplying to avoid int overflow.
struct Rect { int w, h; };
long rect_area(struct Rect r) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.