2d arrays yayayaya
int array_2d[2][3]; // 2 rows and 3 columns.- In memory it’s read left to right line by line
Offset formula
Syntax
You cannot declare a 2D array w/o a set size:
int someMatrix[][]; // NOT allowed! Will not compile
int* someMatrix; // Allowed!Example
char array[2][3] = {
{'A', 'B', 'C'},
{'D', 'E', 'F'},
};| A | B | C |
|---|---|---|
| D | E | F |
| In memory would just be A, B, C, D, E, F. |
I think you could technically do something like
int arr[2][3];
*(*arr+3) = 4; // This would be the second row, first column!? !?!?!?Note!
For the above code, there is a difference between
*(arr + 3) = 4and*(*arr + 3) = 4The top example doesn’t work. Why? Because we cannot “manipulate arrays” as it says by the compiler. What does that mean? Well consider
matrix[2][3]andarray[i] = *(array + i)Using the literal definition of[], we seematrix[2][3] = *(matrix + 2)[3]. Clearly*(matrix + 2)is still an array as we can index [3] from it. That should explain why it gives the error:*(arr + 3)is