I am having some trouble dealing with some C code. Can someone explain this syntax:
void some_function(Int16 omegaFlag[2][8])
{
for(i = 0; i < 2; i++)
{
Int16 *Flag = omegaFlag[i] + 1;
for(j = 0; j < k; j++)
{
// do some stuff
*Flag++ = some_integer_value;
}
}
}
1. Why the parameter Int16 omegaFlag[2][8] passed in some_function() declares index values? How are they helping the code(in general, not specific to this code)?
2. *Flag++ = some_integer_value;: What does this line mean?
The first index to
omegaFlag[2][8], is not required and is ignored by the compiler. The second, however, is important, because it tells the compiler this a two-dimensional array where each row contains 8 elements, so advancing the pointer will advance by 8 elements.omegaFlag[2][8]is equivalent toomegaFlag[][8]or(*omegaFlag)[8]. However it is different than**omegaFlagbecause of the memory layout.**omegaFlagis an array of pointers, whileomegaFlag[2][8]is an array of arrays — with space for exactly 8 elements, or a total space for 16 elements.*Flag++does two things, it de-references Flag, and then also increments its value by 1. Flag is a pointer to and Int16 representing the second value in theith row of the matrix omegaFlag. The assignment assigns a value to that element, and then advances Flag to point to the next element, which will be assigned in the next iteration of the loop.