【发布时间】:2010-02-11 22:28:56
【问题描述】:
我有一个较早的帖子link text有人说我的指针初始化到了错误的元素,我不太明白为什么,除了他们是正确的并且它可以与他们的更正一起使用。所以这是基本问题:
如果我声明一个从 0 到 30 的数组
#define ENDPOINT 15
int record[2 * ENDPOINT + 1];
// and want a pointer to be at the middle of the array, so 0 is at the middle, and
// +15 is at 30, and -15 is at 0
// why is int *ptr = &record[ENDPOINT + 1] wrong? why is the correct declaration
int *ptr = &record[ENDPOINT];
因为如果我将 ptr 放在 &record[ENDPOINT] 处,这意味着记录数组中的第 15 个条目是第 14 个索引,然后添加 15 将只有 29 对吗?谢谢!
【问题讨论】:
-
这里不要使用宏。使用
const int ENDPOINT = 15;或enum { ENDPOINT = 15 };。