[英]Array Index Confusion In C Code
I'm porting some C Code to C#. 我正在将一些C代码移植到C#。 I'm stuck at a piece where I don't quite understand the Author's intention of writing code in unfamiliar fashion.
我被困在一个我不太了解作者以不熟悉的方式编写代码的意图的地方。
The Code is: 该代码是:
typedef struct{
Int32 window[2][8];
Int32 windowF[2][8];
short Index;
}BLOCK_SWITCHING_CONTROL;
maxWindow = SrchMaxWithIndex( &blockSwitchingControl->window[0][8-1],
&blockSwitchingControl->Index, 8);
*****************************************************************************
*
* function name: SrchMaxWithIndex
* description: search for the biggest value in an array
* returns: the max value
*
**********************************************************************************/
static Int32 SrchMaxWithIndex(const Int32 in[], Int16 *index, Int16 n)
{
Int32 max;
Int32 i, idx;
/* Search maximum value in array and return index and value */
max = 0;
idx = 0;
for (i = 0; i < n; i++) {
if (in[i+1] > max) {
max = in[i+1];
idx = i;
}
}
*index = idx;
return(max);
}
As you can see, when SrchMaxWithIndex
is being called, not an array but a single Int32
is being passed as its first parameter which is of course wrong. 如您所见,当
SrchMaxWithIndex
时, SrchMaxWithIndex
不是数组而是单个Int32
作为其第一个参数,这当然是错误的。 But because I know for sure the C code has nothing wrong with it, I'm convinced that I'm missing something here. 但是因为我确定C代码没有错,所以我确信我在这里遗漏了一些东西。 What am I missing?
我想念什么? What was the Author's intention to pass a single
Int32
instead of an array? 作者的意图是传递单个
Int32
而不是数组?
So far I've ported the above to C# in the following manner: 到目前为止,我已经通过以下方式将以上内容移植到了C#中:
static class BLOCK_SWITCHING_CONTROL{
Int32[][] window = new int[2]{new int[8], new int[8]};
Int32[][] windowF = new int[2]{new int[8], new int[8]};
short Index;
};
maxWindow = SrchMaxWithIndex( blockSwitchingControl.window[0]/*[8-1]*/,
out blockSwitchingControl.Index);
*****************************************************************************
*
* function name: SrchMaxWithIndex
* description: search for the biggest value in an array
* returns: the max value
*
**********************************************************************************/
static Int32 SrchMaxWithIndex(Int32 _in[], out Int16 index)
{
Int32 max;
Int32 i, idx;
/* Search maximum value in array and return index and value */
max = 0;
idx = 0;
for (i = 0; i < _in.Length; i++) {
if (in[i+1] > max) {
max = in[i+1];
idx = i;
}
}
index = idx;
return(max);
}
But it is just to remove the errors in C#. 但这只是消除C#中的错误。
The C code is not passing a single integer. C代码未传递单个整数。 It's passing the address of an integer, using the
&
prefix operator. 它使用
&
前缀运算符传递整数的地址 。
There does seem to be some kind of typo though, since the C code references a windowN
member in the struct
which does not seem to exist. 但是,似乎确实存在某种错字,因为C代码引用了似乎不存在的
struct
的windowN
成员。
Assuming it means windowF
, this code: 假设它表示
windowF
,则此代码:
maxWindow = SrchMaxWithIndex(&blockSwitchingControl->windowF[0][8-1],
&blockSwitchingControl->Index, 8);
Tells the called function to treat the given address as an array of 8 integers. 告诉被调用函数将给定地址视为8个整数的数组。 I think this will overflow into
windowF[1][]
. 我认为这将溢出到
windowF[1][]
。 It's very scary code, but if it were as wrong as you state, it wouldn't compile. 这是非常可怕的代码,但是如果您声明的错误率很高,它将无法编译。 You can't in general pass an integer to a function expecting a pointer, in C.
通常,您无法使用C将整数传递给需要指针的函数。
The address of an element of an array is being passed (note the &
in &blockSwitchingControl->windowN[0][8-1]
). 阵列的元素的地址被传递(注意
&
在&blockSwitchingControl->windowN[0][8-1]
So in[i+1]
will be equivalent to blockSwitchingControl->windowN[0][8]
, which is, presumably, a valid item in the array. 因此,
in[i+1]
等同于blockSwitchingControl->windowN[0][8]
,这大概是数组中的有效项。
OK, first, I have to assume you have a mistake in the code: 好的,首先,我必须假设您在代码中有一个错误:
blockSwitchingControl->windowN
Does not exist in the BLOCK_SWITCHING_CONTROL structure. 在BLOCK_SWITCHING_CONTROL结构中不存在。 So I'll answer this assuming you ment something like
windowF
因此,假设您
windowF
类的问题,我将回答这个windowF
Now to your question, the author did pass an array... sort of. 现在您的问题,作者确实通过了一个数组。 Presumably blockSwitchingControl is an structure of type BLOCK_SWITCHING_CONTROL.
大概blockSwitchingControl是BLOCK_SWITCHING_CONTROL类型的结构。 What we're doing here:
我们在这里做什么:
&blockSwitchingControl->windowF[0][8-1]
is passing the address of windowF's [0][7]'th element. 正在传递windowF的第[0] [7]个元素的地址。 A multi-dimensional array is linear (continuous) memory so
Int32 windowF[2][8]
is properly sized for 16 Int32s to be stored in a "row" in memory. 多维数组是线性(连续)内存,因此
Int32 windowF[2][8]
的大小适合16个Int32来存储在内存的“行”中。 Something like: 就像是:
windowF[2][8] => [0][1][2][3][4][5][6][7][8][9][A][B][C][D][E][F][10]
Thus, if I were to pass the address of windowF's [0][7] element, I'm really passing part of the array: 因此,如果我要传递windowF的[0] [7]元素的地址,则实际上是在传递数组的一部分 :
windowF[0][7] [0][1][2][3][4][5][6][7][8][9][A][B][C][D][E][F][10] //full array
-----------------------------^
So now inside the SrchMaxWithIndex() I have _in[] = an array of Int32's which is equivalent to part of windowF's array. 所以现在在SrchMaxWithIndex()中,我有_in [] =一个Int32数组,它等效于windowF数组的一部分。 So you can see they're passing an "array's worth of values", even if it's not how you'd expect.
因此,您可以看到它们正在传递“数组的值”,即使这不是您期望的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.