简体   繁体   English

为指针分配值?

[英]Assigning values to pointers?

In Dennis Ritchie I found this, 在丹尼斯·里奇(Dennis Ritchie)中,我发现了这一点,

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? 它不是struct rect * rp =&r吗?

Yes, it definitely should, as rp is a pointer-to- struct rect , whereas r is of type struct rect . 是的,它绝对应该,因为rp是指向struct rect的指针,而rstruct rect类型。 Maybe a typo in the book? 也许是本书中的错字?

绝对-地址运算符是必需的, 否则会出现编译时错误

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

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". 有问题的书是Kernighan和Ritchie撰写的“ C编程语言”第二版 ,通常称为“ K&R2”。 It's not generally referred to as "Ritchie", since he was just one of the two authors. 由于他只是两位作者之一,因此通常不称为“ Ritchie”。 (Some of you might be interested in knowing that it's now available as a Kindle e-book.) (有些人可能会对知道它现在可以作为Kindle电子书感兴趣)。

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. 后来在1989年10月的印刷中,对第131页(§6.2)进行了较小的更改,以在第208页(§A.17)的最后一个示例( struct rect r, *rp = &r; )上添加& ,以更改“等于在逻辑OR的描述中将“”表示为“不相等”,并在第254页(§B.8)中阐明,对于自动变量,只有声明为volatile变量在setjmp / longjmp序列后才恢复为其最新值。

(And yes, the phrase "automatics variables" should be "automatic variables".) (是的,短语“自动变量”应为“自动变量”。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM