gameloft一考题,新手提问,请多帮助!!!!!
求expr的值a[8][9];
int expr = a[4]-&a[5][7];
请教答案和答案的由来! --------------------编程问答-------------------- The layout of two-dimensional array is actually a sequentially arranged one-dimensional array.
a[4] and &a[5][7] both are pointer pointing to an array element.
When doing pointer arithmetic, the difference is the count of array elements rather than the numerical difference.
The difference is -16 elements, no matter the array is an int[] or char[].
0 0 1 2 3 4 5 6 7 8
1
2 a[4]
3 |
4 . . . . . . . . .
5 . . . . . . . . .
6 |
7 &a[5][7]
By the way, C# doesn't have such construct. A similar way is to
unsafe{
int[,] a = new int[8,9];
fixed (int* p1 = &a[4, 0], p2 = &a[5, 7])
{
int expr = (int)(p1 - p2); // expr = -16
}
}
--------------------编程问答-------------------- a[4]:代表第5维枢组的起始地址
&a[5][7]:代表第6维第8个数的起始地址
每维有9个数
所以 9 + 8 - 1 = 16 个数(第6维第8个数的起始地址实际上前面有7个,第8个并没有在起始地址的包括范围之内)
又因为是小维数减大维数,所以,最后结果为-16
--------------------编程问答-------------------- 学习了,谢谢各位
还是有点蒙,那么换成是a[4]+&a[5][7]呢?
补充:.NET技术 , VC.NET