R..*_*R.. 36
增量和解除引用之间没有排序.但是,*运算符应用于结果p++,即p增量之前的原始值.
Try it. The program
#include <stdio.h>
int main(void) {
int p[2];
int *q = p;
p[0] = 10;
p[1] = 100;
printf("%d\n", *q++);
printf("%d\n", *q);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
prints
10
100
Run Code Online (Sandbox Code Playgroud)
showing that the ++ applies to p, not to *p, and that the increment happens after the dereference.
EDIT: (Thanks to @EricLippert for convincing me to pull out K & R)
Not only may there be a happens-after relationship, but according to K & R page 203, there must be:
后缀表达式后跟 ++ 或 -- 运算符是后缀表达式。表达式的表达式的值就是操作数的值。 记下该值后,操作数将递增 (++) 或递减 (--) 1。
(强调我的)
诚然,我不相信 K & R 在存在多线程的情况下对 C 的语义有任何说明(根据Wikipedia,pthreads 规范于 1995 年发布),但对于单线程程序,K & R 非常清楚。
| 归档时间: |
|
| 查看次数: |
24451 次 |
| 最近记录: |