140 likes | 280 Views
This document provides an overview and review of key concepts from K&R Chapter 6, focusing on structs and their implementation in C programming. Topics include defining struct.point and struct.rect, calculating areas, comparing structs, and using pointers to structs. It also covers arrays of structs, initialization, size determination of structs, and the ptinrect function for point-in-rectangle checks. Ideal for exam preparation, this open-book review will help solidify your understanding of data structures in C programming.
E N D
Homework / Exam • Continuing K&R Chapter 6 • Exam 2 after next class • Open Book / Open Notes • Up through end of K&R 6.4 plus MAKE
Review structs struct point { int x; int y; } pt; struct point pt1, pt2; struct point maxpt = {320, 200};
Review structs struct rect { struct point pt1; struct point pt2; }; Note: must have ; following } struct rect box;
Review structs box.pt1.x = 5; box.pt1.y = 10; box.pt2.x = 10; box.pt2.y = 20; area = (box.pt2.x – box.pt1.x) * (box.pt2.y – box.pt1.y);
What can we do with a struct? • Reference members box.pt2.x = box.pt1.x + width; • Assign as a unit pt2 = pt1; • Create a pointer to it struct point *ppt1; ppt1 = &pt1;
What can we do with a struct? • Not legal to compare structs if (pt1 == pt2) … INVALID • Must be done as: if (pt1.x == pt2.x && pt1.y == pt2.y) …
structs and Functions, K&R 6.2 /* ptinrect: if point p in rect r, return 1 else return 0 note: slightly different from K&R example */ int ptinrect (struct point p, struct rect r) { return p.x >= r.pt1.x && p.x <= r.pt2.x && p.y >= r.pt1.y && p.y <= r.pt2.y; }
Arrays of structs, K&R 6.3 • Multiple related arrays char * keyword[NKEYS]; int keycount[NKEYS]; • Can be implemented as an array of structs struct key { char *word; int count; } keytab [NKEYS];
Arrays of structs • Alternative array of structs implementation struct key { char *word; int count; }; struct key keytab[NKEYS];
Arrays of structs • Initialization for an array of structs struct key { char *word; int count; } keytab[ ] = { “auto”, 0, … “while”, 0 }; /* NKEYS is dynamically derived */
Size of structs • sizeof is a compile-time unary operator • Can be used to get integer (actually size_t): sizeof object sizeof (type name) • Applied to structs #define NKEYS (sizeof keytab / sizeof (struct key)) #define NKEYS (sizeof keytab / sizeof keytab[0])
Pointers to structs, K&R 6.4 • Declare and initialize a pointer to struct struct point p; struct point *pp = &p; • Refer to members of struct p via pointer pp (*pp).x and (*pp).y See precedence • More commonly done as: pp->x and pp->y See precedence
Pointers to structs struct string { int len; char *cp; } *p; Expression Same as Value / Effect ++p->len ++(p->len) increments len *p->cp *(p->cp) value is a char *p->cp++ *((p->cp)++) value is a char increments cp
Pointers to structs /* ptinrect: (pointer version) if point p in rect r, return 1 else return 0 */ int ptinrect (struct point *pp, struct rect *rp) { return pp->x >= rp->pt1.x && pp->x <= rp->pt2.x && pp->y >= rp->pt1.y && pp->y <= rp->pt2.y; }