-1

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?

Sean Vaughn
  • 3,792
  • 1
  • 16
  • 14
  • 1
    What is `n` in the `for` loop? `i` isn't declared within the function either. Are these global variables? If not, please show code that will actually compile. – Praetorian Sep 15 '12 at 15:10
  • *Flag++ = some_integer_value; gets some_integer into *Flag then increases the adress Flag points to. First it gives a lvalue as *Flag then when it assigns it increases the adress – huseyin tugrul buyukisik Sep 15 '12 at 15:10
  • `n` is any value. It's omitted because it doesn't concerns the code understandability. @Prætorian – Sean Vaughn Sep 15 '12 at 15:12
  • 1
    Have I just gone insane - where is `i, n, j, k` declared - and why the hell make code so unreadable? – Ed Heal Sep 15 '12 at 15:13
  • possible duplicate of [What is the difference between array\[ROWS\]\[COLS\] and **array](http://stackoverflow.com/questions/12330170/what-is-the-difference-between-arrayrowscols-and-array) – Alok Save Sep 15 '12 at 15:14

3 Answers3

3

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 to omegaFlag[][8] or (*omegaFlag)[8]. However it is different than **omegaFlag because of the memory layout. **omegaFlag is an array of pointers, while omegaFlag[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 the ith 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.

epsalon
  • 2,294
  • 12
  • 19
1

The array omegaFlag is modified using a pointer. Of course, this is needless use of pointers when you can write it like this, which is much easier to read:

 for(i = 0; i < n; i++)
    {
        for(j = 1; j < k; j++)
        {
            // do some stuff
            OmegaFlag[i][j] = some_integer_value;
        }
    }

Of course accessing outside the bounds of the array is undefined behaviour i.e. n>2 Or k>7.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • This should be `OmegaFlag[i][j+1]` due to the initialization of `Flag`. – epsalon Sep 15 '12 at 15:15
  • `omegaFlag[i+1]` would be equivalent to starting an iteration with i=1. `omegaFlag[i] + 1` is equivalent to advancing Flag once before the loop, or using `j+1` as the column index. – epsalon Sep 15 '12 at 15:21
  • @epsalon Right, I changed the index. But don't delete the line below with your edits. – P.P Sep 15 '12 at 15:25
  • almost correct, but now the top index of the inside for should be `k+1`. – epsalon Sep 15 '12 at 17:00
1
  1. The parameter declaration advises that the function will receive a array of Int16 in the given dimensions...more specifically, an array of arrays.

  2. The * symbol indicates a pointer, or a location in memory. *Flag means "the location pointed to by the contents of Flag", and the assignment means "put this value in that location." The ++ indicates that the location pointed to by Flag should be increased by one after it is used, eg if it was pointing to memory location 12345, it will then point to location 12346. Arrays can be referenced via pointers in this way, so the inbound array is being modified by this syntax... although it can make it tough to read :)

Hope that helps.

David W
  • 10,062
  • 34
  • 60