5 ms·
I'm confused about your reference to C. C has multidimensional arrays, and all the elements are laid out contiguously in memory. No index calculations are neede
by zwegner 13y ago
I'm confused about your reference to C. C has multidimensional arrays, and all the elements are laid out contiguously in memory. No index calculations are needed, it's all done by the array indexing operation.
If you have this code:
int a[10][10];
then:
&a[x][y] == ((char *)a + x * sizeof(a[0]) + y * sizeof(a[0][0]))
where sizeof(a[0]) is == sizeof(int) * 10.
quick edit: added a (char *) cast so the arithmetic doesn't multiply by sizeof(int) again
- StefanKarpinski 13y agoRight, I should have clarified. That works for statically allocated arrays but not for dynamically allocated ones. Since matrix dimensions are generally not known ahead of time, in real programs you almost always end up needing to simulate multiple indices using explicit index arithmetic.