I’m porting some C Code to 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. But because I know for sure the C code has nothing wrong with it, I’m convinced that I’m missing something here.
What am I missing? What was the Author’s intention to pass a single Int32 instead of an array?
So far I’ve ported the above to C# in the following manner:
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#.
OK, first, I have to assume you have a mistake in the code:
Does not exist in the BLOCK_SWITCHING_CONTROL structure. So I’ll answer this assuming you ment something like
windowFNow to your question, the author did pass an array… sort of.
Presumably blockSwitchingControl is an structure of type BLOCK_SWITCHING_CONTROL. What we’re doing here:
is passing the address of windowF’s [0][7]’th element. 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. Something like:Thus, if I were to pass the address of windowF’s [0][7] element, I’m really passing part of the array:
So now inside the SrchMaxWithIndex() I have _in[] = an array of Int32’s which is equivalent to part of windowF’s array. So you can see they’re passing an “array’s worth of values”, even if it’s not how you’d expect.