我可以使用指针访问多维数组吗? [英] Can I access multidimensional array using a pointer?
问题描述
来自 this 参考,在 C 中似乎以下行为未定义.>
From this reference, In C it seems the following behavior is undefined.
int my_array[100][50];
int *p = my_array[0];
p[50]; // UB
C++03 或 C++11 中是否有参考文献证实了这一点?
Is there a reference in C++03 or C++11 which confirms this?
推荐答案
在 + 运算符的描述中是.您不能在 C 中取消引用该指针,因为它超过了第一个子数组的结束指针.在 C++ 中,这目前是合法的,因为指针 points 指向一个有效的整数(指向关系的点在第 3 节中的某处定义).然而,在这两个标准中,添加超过 50 个会产生未定义的行为.
Yes in the description of the + operator. You may not dereference that pointer in C because it is a past the end pointer for the first subarray. In C++ this currently is legal because the pointer points to a valid integer (the points to relation is defined somewhere in clause 3). However in both standards adding more than 50 yields undefined behavior.
最近向 C++ 委员会发送了一份 DR,内容涉及取消引用此类凭空有效"指针可能会取消引用的规则,因此我不会依赖该规则.
A DR was recently sent to the c++ committee about the rule that dereferencing such "valid out of thin air" pointers may be dereferenced, so i would not rely on that.
这篇关于我可以使用指针访问多维数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!