Apr 14, 20232D Arrays in C Tutorial 1 - Creating a 2D ArrayStarting the guide on 2D Arrays in C!
William Huynh βΈ± 2 min read
2D Arrays in C π₯³
Welcome to our guide on 2D Arrays in C!
Here you will learn how to use and understand seamlessly the very scary topic that is 2D arrays!
To break it down nice and easy, we will be covering 6 operations:
- Creating a 2D Array π§
- Initialising a 2D Array π
- Accessing elements in a 2D Array π
- Editing elements in a 2D Array βοΈ
- Iterating through a 2D Array π
Creating a 2D Array π§
Preface πΆ
A 2D array is often referred to as an array of arrays, or a matrix! This is because it consist of rows and columns, and thus takes the shape of a matrix!
Getting Started π
To create a 2D array in C, you will need to declare it using the following syntax:
<datatype> <arrayname> [row_size][col_size];
Where:
<datatype>
is the datatype of the array (i.e.int
,char
,bool
, etc...)<arrayname>
is the name of the array (i.e.my_array
)row_size
is the number of rows in the arraycol_size
is the number of columns in the array
For example, to create a 2D array of integers with 2 rows and 4 columns, you would use the following declaration:
int my_array[2][4];
Pretty easy right?