Ryan Chia Ryan Chia - 12 days ago 6
C Question

C 2D Array changes 2 elements at the same time

This is my 2D array

int myarray[79][24];
Whenever I change an element at row 0, it changes another element row 24 at the same time and vice versa.

For example,
myarray[35][0] = 'X';
changes
myarray[35][0]
and
myarray[34][24]
to X at the same time. And when I tried
myarray[40][24] = 'X';
it changes
myarray[40][24]
and
myarray[41][0]
at the same time.

It seems like the first and last row are mirroring each other. How can I stop this from happening?

Answer

Specification: 6.5.2.1 Array subscripting

Successive subscript operators designate an element of a multidimensional array object. If E is an n-dimensional array (n ≥ 2) with dimensions i × j × ... × k, then E (used as other than an lvalue) is converted to a pointer to an (n − 1)-dimensional array with dimensions j × ... × k. If the unary * operator is applied to this pointer explicitly, or implicitly as a result of subscripting, the result is the referenced (n − 1)-dimensional array, which itself is converted into a pointer if used as other than an lvalue. It follows from this that arrays are stored in row-major order (last subscript varies fastest).

C is a language which allows the programmer to do a lot. For example, there is no IndexOutOfBoundsError. An array is just a pointer to the first cell and then it has reservation for array length next cells.

When you declare int[79][24] it makes a reservation for 79*24 cells. They go one by one. If the array dimensions are 79x24, you have indexes from 0-78 in the first dimension and 0-23 in the second. The cell at the adress 34,24 is actually one after 34,23 which is 35,0 in the memory.

Let's have an example on array of size 6x4:

Your array represented as expected by you with values:
+---+---+---+---+---+---+---+
|   | 0 | 1 | 2 | 3 | 4 | 5 |
+---+---+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+
| 1 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+
| 2 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+
| 3 | 0 | 0 | 0 | 0 | 0 | 0 |
+---+---+---+---+---+---+---+

Your array with cell adresses (where start is the adress of cell 0,0):
+---+------------+------------+------------+------------+------------+------------+
|   | 0          | 1          | 2          | 3          | 4          | 5          |
+---+------------+------------+------------+------------+------------+------------+
| 0 | start      | start + 1  | start + 2  | start + 3  | start + 4  | start + 5  |
+---+------------+------------+------------+------------+------------+------------+
| 1 | start + 6  | start + 7  | start + 8  | start + 9  | start + 10 | start + 11 |
+---+------------+------------+------------+------------+------------+------------+
| 2 | start + 12 | start + 13 | start + 14 | start + 15 | start + 16 | start + 17 |
+---+------------+------------+------------+------------+------------+------------+
| 3 | start + 18 | start + 19 | start + 20 | start + 21 | start + 22 | start + 23 |
+---+------------+------------+------------+------------+------------+------------+
Comments