Course Content
C Basics
C Basics
Two Dimensional Arrays
Two-dimensional arrays are a bit like tables — think of them as arrays within arrays, with rows and columns.
Declaring a two-dimensional array requires an extra set of square brackets:
Imagine a table where:
- Row
a[0]...
represents the first set of data or the first array; - Row
a[1]...
is the second set; - Row
a[2]...
is the third.
When an array is nested within another array, it's referred to as a "nested array."
Accessing the elements inside these nested arrays requires using indices along with an additional set of brackets:
main
#include <stdio.h> int main() { // dimensional array declaration int array[3][4] = { {1, 11, 111, 1111}, // index 0 {2, 22, 222, 2222}, // index 1 {3, 33, 333, 3333} // index 2 }; printf("%d", array[1][0]); // print the first element of the second nested array return 0; }
Note
Why do we need to specify the data type and size of an array? It helps the compiler understand how much memory should be reserved for your data. Remember: all elements within an array must be of the same type!
To really tap into the power of arrays, upcoming lessons will introduce loops. Loops allow us to automate tasks with arrays, like displaying each element in succession (or based on a certain pattern) or inputting text into arrays. Now, you might wonder, why would we need text in arrays? Stay tuned for the next lesson to find out!
Thanks for your feedback!