#include <stdio.h>
struct point {/* NO STORAGE ALLOC */
int x;
int y;
};
struct rect {/* NO STORAGE ALLOC */
struct point pt1; struct point pt2; };
int width(struct rect ); int height(struct rect );
int main() { struct rect box; box.pt1.x = 3; box.pt1.y = 2; box.pt2.x = 11; box.pt2.y = 7; printf("width: %i, height: %i
", width(box), height(box)); }
int width(struct rect r) { return abs(r.pt1.x - r.pt2.x); }
int height(struct rect r) { return abs(r.pt1.y - r.pt2.y); }
|
#include <stdio.h>
typedef struct {/* NO STORAGE ALLOC */
int x;
int y;
} point;
typedef struct {/* NO STORAGE ALLOC */
point pt1;
point pt2;
} rect;
int width(rect ); int height(rect );
int main() { rect box; box.pt1.x = 3; box.pt1.y = 2; box.pt2.x = 11; box.pt2.y = 7; printf("width: %i, height: %i
", width(box), height(box)); }
int width(rect r) { return abs(r.pt1.x - r.pt2.x); }
int height(rect r) { return abs(r.pt1.y - r.pt2.y); }
|
#include <stdio.h> #define abs(x) (x > 0 ? (x) : -(x)) struct point {/* NO STORAGE ALLOC */
int x;
int y;
};
struct rect {/* NO STORAGE ALLOC */
point pt1;/* "struct" optional */
point pt2;/* "struct" optional */
};
int width(rect );/* "struct" opt */
int height(rect );/* "struct" opt */
int main() {
rect box;/* "struct" optional */
box.pt1.x = 3;
box.pt1.y = 2;
box.pt2.x = 11;
box.pt2.y = 7;
printf("width: %i, height: %i
",
width(box), height(box));
}
int width(rect r) {/* "struct" opt */
return abs(r.pt1.x - r.pt2.x);
}
int height(rect r) {/* "struct" opt */
return abs(r.pt1.y - r.pt2.y);
}
|
% cc -o boxes boxes.c
% boxes
width: 8, height: 5
|
% CC -o boxes3 boxes3.c
% boxes3
width: 8, height: 5
|