Apr 14, 20232D Arrays in C Tutorial 4Editing elements in a 2D Arrays
William Huynh βΈ± 2 min read
Editing elements in a 2D Array βοΈ
Preface πΆ
So we've learnt how to access elements in an array, but whats also cool is that we can change those elements too!
Getting Started π
To edit an element in a 2D array you need two things!
- The row index
- The column index
With those two things we can access and thus edit any element!
Remember that indexes start at
0
, so the first row and first column will have indexes of0
.
Say we have the following array:
int my_array[2][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8} // Second row
};
We know that to access element 2
, we can do the following:
int my_num = my_array[0][1];
Luckily, editing it is very similar!
Let say we want to change the element at [0][1]
to be 42, we can do the following:
my_array[0][1] = 42;
Pretty cool right? Onto the final topic we go!