更新时间:2023-11-08 09:38:22
尼尔的答案是在一般情况下比较好,但如果你有使用普通的旧数组的一个原因,你可以使用函数来获取和设置的值,也请确认您使用的数组边界内的:
Neil's answer is better in the general case, but if you have a reason for using plain old arrays, you can use functions to get and set the values and also check that you're within the array bounds:
#define MAX_INDEX 10
int y[MAX_INDEX][MAX_INDEX][MAX_INDEX];
int get_y(int a, int b, int c)
{
ASSERT(a >= 0 && a < MAX_INDEX);
ASSERT(b >= 0 && b < MAX_INDEX);
ASSERT(c >= 0 && c < MAX_INDEX);
return y[a][b][c];
}
void set_y(int a, int b, int c, int value)
{
ASSERT(a >= 0 && a < MAX_INDEX);
ASSERT(b >= 0 && b < MAX_INDEX);
ASSERT(c >= 0 && c < MAX_INDEX);
y[a][b][c] = value;
}
......都包裹在一个类中,理想的。
...all wrapped up in a class, ideally.