为什么只有第二阵列尺寸重要吗? [英] Why is only the second array dimension important?

查看:99
本文介绍了为什么只有第二阵列尺寸重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么只有两个二维数组第二维工作时是一个编译器很重要?总不能让我周围的头。
谢谢

Why when working with two dimensional arrays only second dimension is important for a compiler? Just can't get my head around that. Thanks

推荐答案

由于编译器需要弄清楚如何从内存中访问数据。第一尺寸并不重要,因为当给定的所有其他尺寸编译器可以计数的项数。

Because compiler needs to figure out how to access the data from memory. The first dimension is not important because compiler can count the number of items when all other sizes are given.

例如:

int a1[] = { 1, 2, 3, 4 }

编译器知道要为4个整数分配空间。现在,有了这个:

compiler knows to allocate space for 4 integers. Now, with this:

int a2[][] = { 1, 2, 3, 4, 5, 6} }

编译器不能决定是否应该A2 [1] [6]或A2 [2] [3]或A2 [3] [2]或A2 [6] [1]。一旦你告诉它的第二个方面,它可以计算第一个。

compiler cannot decide whether it should be a2[1][6] or a2[2][3] or a2[3][2] or a2[6][1]. Once you tell it the second dimension, it can calculate the first one.

例如,试图访问单元A2 [1] [0]会产生视声明不同的值。你可以得到2,3,4,甚至无效的位置。

For example, trying to access element a2[1][0] would yield different values depending on the declaration. You could get 2, 3, 4 or even invalid position.

这篇关于为什么只有第二阵列尺寸重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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