| I can't understand C matrices!

Previous | Memory, C Variables, and Pointers | Next

Q: | I can't understand matrices in C (not TI-Basic matrices, but matrices which are the part of the C language)!
A: | Basically, to declare a matrix, you need to use:
    type name[dimension1][dimension2];
for example:
    int a[5][5];
And, to access element of the matrix, you need to use:
    name[index1][index2];
for example:
    a[2][3] = 10;
or
    x = a[1][2];
But note that indices are not from 1 to dimension, but from 0 to dimension-1. So, in the above example, both indices are in range 0 to 4, not 1 to 5. For example to fill all matrix by zeroes, you can use this code:
    int a[5][5], i, j;     // A 5x5 array of ints, and two single ints

    for(i = 0; i < 5; i++)
      for (j = 0; j < 5; j++)
        a[i][j] = 0;
although the experienced C programmer will simply use
    memset (a, 0, 5 * 5 * sizeof(int));
to make it faster.