Example 1 — Rectangle made of two points
Compute width, height, and area from the corners.
#include <stdio.h>
struct Point { int x, y; };
struct Rect { struct Point tl, br; };
int main(void) {
struct Rect r = {{0, 0}, {4, 3}};
int w = r.br.x - r.tl.x;
int h = r.br.y - r.tl.y;
printf("width=%d height=%d area=%d\n", w, h, w * h);
return 0;
} How It Works
tlandbrarestruct Pointmembers.r.br.xreads the x of the bottom-right corner.- Nested braces initialize the inner structs.
