我正在阅读卡内基梅隆大学的《计算机系统》幻灯片以备我的小测验。在第49页幻灯片中:
Counting Down with Unsigned
Proper way to use unsigned as loop index
unsigned i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1];
Even better
size_t i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1];
我不明白为什么这不会变成无限循环。我正在减少 i
,而它是无符号的,所以应该始终小于 cnt
。请解释。
unsigned i = cnt - 1; while (i--) a[i] = a[i+1];
有什么问题吗? - David C. Rankin