homework exam n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Homework / Exam PowerPoint Presentation
Download Presentation
Homework / Exam

Loading in 2 Seconds...

play fullscreen
1 / 14

Homework / Exam - PowerPoint PPT Presentation


  • 76 Views
  • Uploaded on

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;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Homework / Exam' - laban


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
homework exam
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
Review structs

struct point {

int x;

int y;

} pt;

struct point pt1, pt2;

struct point maxpt = {320, 200};

review structs1
Review structs

struct rect {

struct point pt1;

struct point pt2;

};  Note: must have ; following }

struct rect box;

review structs2
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
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 struct1
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
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
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
Arrays of structs
  • Alternative array of structs implementation

struct key {

char *word;

int count;

};

struct key keytab[NKEYS];

arrays of structs1
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
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
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
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 structs1
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;

}