The code in figure 325 lets us illustrate a number of

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: TATION OF C PROGRAMS struct rect r; r.llx = r.lly = 0; r.color = 0xFF00FF; r.width = 10; r.height = 20; where the expression r.llx selects field llx of structure r. It is common to pass pointers to structures from one place to another rather than copying them. For example, the following function computes the area of a rectangle, where a pointer to the rectange struct is passed to the function: int area(struct rect *rp) { return (*rp).width * (*rp).height; } The expression (*rp).width dereferences the pointer and selects the width field of the resulting structure. Parentheses are required, because the compiler would interpret the expression *rp.width as *(rp.width), which is not valid. This combination of dereferencing and field selection is so common that C provides an alternative notation using ->. That is, rp->width is equivalent to the expression (*rp).width. For example, we could write a function that rotates a rectangle left by 90 degrees as void rotate_left(struct rect *rp) { /* Exchange width and height */ int t = rp->height; rp->height = rp->width;...
View Full Document

Ask a homework question - tutors are online