Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Two Dimensional Arrays | Data
C Basics
course content

Course Content

C Basics

C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

bookTwo 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:

c

main

copy
123456789101112131415
#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!

What is the output of this code?

What is the output of this code?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt