数组边界和索引

Cry*_*tal 0 c++ arrays pointers

我有一个较早的帖子链接文本,有人说我的指针初始化为错误的元素,我不明白为什么,除了他们是正确的,它适用于他们的纠正.所以这是基本问题:

如果我声明一个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];
Run Code Online (Sandbox Code Playgroud)

因为如果我将ptr放在&record [ENDPOINT],这意味着记录数组中第15个条目是第14个索引,然后添加15将只有29个对吗?谢谢!

fbr*_*eto 6

record[ENDPOINT]是第16个元素 - 数组的索引从0开始,record[0]第1个元素record[1]也是第2个元素,而record[ENDPOINT](是record[15])是第16个元素.

2*15+1数组中有31个元素.向ENDPOINT(15)添加14会产生29,并且record[29]是数组中的第30个元素.record[30]将是数组中的最后一个或第31个元素.