Question

In Dennis Ritchie I found this,

struct rect r , *rp = r;

then these four expressions are equivalent :

  1. r.pt1.x
  2. rp->pt1.x
  3. (r.pt1).x
  4. (rp->pt1).x

because operators associate left to right.
Shouldn't it be struct rect *rp = &r?

Was it helpful?

Solution

Absolutely -- the address operator is required, otherwise you get a compile-time error:

incompatible types when initializing type ‘struct rect *’ using type ‘struct rect’

OTHER TIPS

Yes, it definitely should, as rp is a pointer-to-struct rect, whereas r is of type struct rect. Maybe a typo in the book?

It was a typo in the book, and it's been corrected in later editions.

The book in question is "The C Programming Language", 2nd Edition, by Kernighan and Ritchie, commonly referred to as "K&R2". It's not generally referred to as "Ritchie", since he was just one of the two authors. (Some of you might be interested in knowing that it's now available as a Kindle e-book.)

The errata list for the book says:

A later printing in October, 1989, made minor changes on page 131(§6.2) to add & to the last example ( struct rect r, *rp = &r;), on page 208(§A.17) to change "equal" to "unequal" in the description of logical OR, and on page 254(§B.8) to clarify that for automatics variables, only those declared volatile are restored to their latest values after a setjmp / longjmp sequence.

(And yes, the phrase "automatics variables" should be "automatic variables".)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top