我可以把一个二维数组作为一个连续的一维数组? [英] May I treat a 2D array as a contiguous 1D array?

查看:139
本文介绍了我可以把一个二维数组作为一个连续的一维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code:

int a[25][80];
a[0][1234] = 56;
int* p = &a[0][0];
p[1234] = 56;

请问第二行调用未定义行为?如何第四行?

Does the second line invoke undefined behavior? How about the fourth line?

推荐答案

这是由跨pretation。而数组的连续性要求不留下太多的想象中,如何布置一个多维数组项(这已经指出之前),请注意,当你做 P [1234] 你只索引80列的第零行的元素1234。一些跨preT唯一有效的指标是0..79(&安培; P [80] 是一种特殊情况)。

It's up to interpretation. While the contiguity requirements of arrays don't leave much to the imagination in terms of how to layout a multidimensional arrays (this has been pointed out before), notice that when you're doing p[1234] you're indexing the 1234th element of the zeroth row of only 80 columns. Some interpret the only valid indices to be 0..79 (&p[80] being a special case).

从信息Ç常见问题解答这是新闻组就有关事项C.收集智慧(我做的没想到C和C ++对此事有所不同,这是非常相关的。)

Information from the C FAQ which is the collected wisdom of Usenet on matters relevant to C. (I do not think C and C++ differ on that matter and that this is very much relevant.)

这篇关于我可以把一个二维数组作为一个连续的一维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆