Multi-Dimensional Arrays in C Programming: Definition & Example

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: How to Create, Use & Manipulate Strings in C Programming

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Multi-Dimensional Arrays
  • 1:40 Initializing & Accessing
  • 2:15 3-Dimensional Arrays
  • 3:06 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Login or Sign up

Timeline
Autoplay
Autoplay
Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In C programming, you can create multi-dimensional arrays, which are very useful. This lesson defines the most common types of multi-dimensional arrays and provides working code examples.

Multi-Dimensional Arrays

We have entered a new dimension. Or perhaps several! The only benefit (or drawback) is that we won't be leaving Earth; instead we will be adding complexity and depth to our arrays in C.

A multi-dimensional array is an array that has more than one dimension. It is an array of arrays; an array that has multiple levels. The simplest multi-dimensional array is the 2D array, or two-dimensional array. It's technically an array of arrays, as you will see in the code. A 2D array is also called a matrix, or a table of rows and columns.

Declaring a multi-dimensional array is similar to the one-dimensional arrays. For a 2D array, we need to tell C that we have 2 dimensions.

This example declares a 2D integer array:

int two_d[3][3];

A valid type is required (in this case int), followed by the name of the array, and the size of each dimension. In this case, we created a 2D array that's 3 by 3 (three rows and three columns). As stored in the computer's memory, the array looks like this table. Remember that C starts counting all buckets at 0!

Column 0 Column 1 Column 2
Row 0 two_d[0][0] two_d[0][1] two_d[0][2]
Row 1 two_d[1][0] two_d[1][1] two_d[1][2]
Row 2 two_d[2][0] two_d[2][1] two_d[2][2]

As mentioned before, the 2D array is a matrix. Therefore, it's well-suited for spreadsheet-type data (e.g., period table, list of employees and their attributes, lab test results, etc.).

For example, we could create a 2D array for 50 students to capture scores on four tests:

int gradebook[50][4];

Recall that C starts counting at 0! Therefore, the 4th test for Student 25 can be accessed in this manner:

gradebook[24][3];

Keep the concept of a table in mind: In this case, there are 50 rows and 4 columns in the table.

Initializing and Accessing Array Elements

The most readable option to initialize arrays is shown here. This code shows how to pre-fill a 2x3 2D array, that is an array with 2 rows and 3 columns:

int two_d[2][3] = {
 {14, 12, 10},
 {8, 6, 4}
};

In order to step through a multi-dimensional array, we'll need to use a nested for loop. Think of the example of the matrix: Start across the first row, then work across each column; when done, move to the next row.

First, we create two counting variables (i and j) to step through:

int i,j = 0;

Next, create the for loops to loop through. The counter i represents rows; j represents columns:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support