structs:
|
"Traditional" structs in C | "typedef" structs in C | structs in C++ |
---|---|---|
#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\n", 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\n", 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\n", 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); } |
Common Output | C++ Output | |
% cc -o boxes boxes.c % boxes width: 8, height: 5 | % CC -o boxes3 boxes3.c % boxes3 width: 8, height: 5 |
Equivalent Expressions |
---|
(*temp).x |
Using extra function | Using pointers to structs |
---|---|
#include <stdio.h> struct point { int x; int y; }; struct rect { struct point pt1; struct point pt2; }; int width(struct rect ); int height(struct rect ); struct point makepoint(int , int ); int main() { struct rect box; box.pt1 = makepoint(3, 2); box.pt2 = makepoint(11, 7); printf("width: %i, height: %i\n", 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); } struct point makepoint(int x, int y) { struct point temp; temp.x = x; temp.y = y; return temp; } |
#include <stdio.h> struct point { int x; int y; }; struct rect { struct point pt1; struct point pt2; }; int width(struct rect ); int height(struct rect ); struct point *makepoint(int , int ); int main() { struct rect box; box.pt1 = *makepoint(3, 2); box.pt2 = *makepoint(11, 7); printf("width: %i, height: %i\n", 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); } struct point *makepoint(int x, int y) { struct point *temp = (struct point *) malloc(sizeof(struct point)); (*temp).x = x; /* temp -> x = x */ (*temp).y = y; /* temp -> y = y */ return temp; } |
Common Output | |
% cc -o Boxes Boxes.c % Boxes width: 8, height: 5 |