I have some C code that works great, but I do not understand one part of the code. In thi开发者_如何学Cs part it passes a method two pointers to a two dimensional array with only one dimension specified.
Here it is:
if (cmppt(pts[i*3], pts[hull*3]))
hull = i;
the method cmppt looks like this:
inline bool cmppt(const float* a, const float* b) {
if (a[0] < b[0]) return true;
if (a[0] > b[0]) return false;
if (a[2] < b[2]) return true;
if (a[2] > b[2]) return false;
return false;
}
The array pts is a two-dimensional array. My question is, when the method is passed both arrays for example pts[3] and pts[0] how do I know what part of the array is it looking at? It seems that pts[0] looks at the first element of the first dimension of the array and pts[3] look at the second element of the first dimension of the array, but this doesn't make sense.
Thank you
pts[i*3]
is equivalent to &pts[i*3][0]
, which is a pointer to the first element of the i*3
rd row of the 2D array. Likewise, pts[hull*3]
is a pointer to the first element of the hull*3
rd row of the array.
So, the function cmppt
takes pointers to two rows of an array. It doesn't know which rows it's looking at, it only knows that it is looking at rows.
its because when you say pts[i*3] what you are really saying is pts[i*3][]. 2 dimentional arrays have rows and columns... you passed it the row.
your array (jagged) looks like this:
int[][] pts =
{
new int[]{ 0, 1, 2, 3, 4, 5},
new int[]{ 0, 1, 2, 3, 4, 5},
new int[]{ 0, 1, 2, 3, 4, 5},
new int[]{ 0, 1, 2, 3, 4, 5},
new int[]{ 0, 1, 2, 3, 4, 5},
new int[]{ 0, 1, 2, 3, 4, 5}
};
then you pass it in to the function like this basically:
cmppt(int[] a, int[] b)
精彩评论