Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements

Article by: Manish Methani

Last Updated: November 2, 2021 at 2:04pm IST
4 mins 54 sec read

Certainly! In this tutorial, we'll cover the basics of two-dimensional arrays in C programming, including their definition, initialization, accessing elements, and storage in memory. We'll also provide some code examples to help you better understand how they work.

What are Two-Dimensional Arrays in C Programming?

Two-dimensional arrays in C programming are arrays with two dimensions, usually represented as rows and columns. Each element in the array can be accessed by its row and column index. They are useful for representing tables, matrices, and other two-dimensional data structures.

How to Declare and Initialize Two-Dimensional Arrays in C Programming

To declare a two-dimensional array in C programming, you need to specify the data type of the elements, the number of rows, and the number of columns. Here's an example of how to declare a two-dimensional array of integers with three rows and four columns:

int myMatrix[3][4];

To initialize the array, you can use the curly braces syntax to assign values to each element. Here's an example of how to initialize the same array with values:

int myMatrix[3][4] = {
   {10, 20, 30, 40},
   {50, 60, 70, 80},
   {90, 100, 110, 120}
};

You can also initialize a two-dimensional array partially, leaving some elements with default values of 0. Here's an example of how to initialize the first two rows of the same array:

int myMatrix[3][4] = {
   {10, 20, 30, 40},
   {50, 60, 70, 80}
};

How to Access Elements in Two-Dimensional Arrays in C Programming

To access an element in a two-dimensional array, you need to use its row and column index within square brackets after the array name. Here's an example of how to access the element in the second row and third column of the same array:

int element = myMatrix[1][2];

You can also assign a new value to an element in a two-dimensional array using its row and column index. Here's an example of how to assign a new value of 100 to the element in the third row and fourth column of the same array:

myMatrix[2][3] = 100;

How Two-Dimensional Arrays are Stored in Memory

Two-dimensional arrays in C programming are stored in memory in either row-major order or column-major order. In row-major order, the elements in the same row are stored together in memory, followed by the elements in the next row, and so on. In column-major order, the elements in the same column are stored together in memory, followed by the elements in the next column, and so on.

Here's an example of how to print the memory addresses of the elements in a two-dimensional array in row-major order:

for(int i = 0; i < 3; i++) {
   for(int j = 0; j < 4; j++) {
      printf("%p ", &myMatrix[i][j]);
   }
   printf("");
}

And here's an example of how to print the memory addresses of the elements in the same array in column-major order:

for(int j = 0; j < 4; j++) {
   for(int i = 0; i < 3; i++) {
      printf("%p ", &myMatrix[i][j]);
   }
   printf(" ");
}

Example

Here's a complete program on 2D arrays in C with code and an explanation.

#include <stdio.h>

int main()
{
    // Declare a 2D array of integers
    int arr[3][3];
    
    // Initialize the 2D array with some values
    arr[0][0] = 1;
    arr[0][1] = 2;
    arr[0][2] = 3;
    arr[1][0] = 4;
    arr[1][1] = 5;
    arr[1][2] = 6;
    arr[2][0] = 7;
    arr[2][1] = 8;
    arr[2][2] = 9;
    
    // Print the elements of the 2D array
    printf("The elements of the 2D array are:");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", arr[i][j]);
        }
        printf("");
    }
    
    return 0;
}

Explanation:

  • We start by declaring a 2D array of integers named arr with 3 rows and 3 columns using the syntax int arr[3][3].
  • We then initialize the 2D array with some values. We use the syntax arr[i][j] = value to assign a value to each element of the array.
  • Next, we use a nested for loop to print the elements of the 2D array. We first loop through the rows using the variable i, and then loop through the columns using the variable j. We use the syntax printf("%d ", arr[i][j]) to print each element of the array, and printf(" ") to move to a new line after printing all the elements of a row.
  • Finally, we return 0 to indicate that the program has executed successfully.

This program declares, initializes, and prints the elements of a 2D array of integers in C. You can modify the values of the elements or the size of the array to suit your needs.

In conclusion, two-dimensional arrays are an essential data structure in C programming, especially when working with matrices, tables, and other two-dimensional data. We learned how to declare, initialize, and access elements in two-dimensional arrays in C programming, as well as how they are stored in memory in either row-major order or column-major order.

By using two-dimensional arrays, we can organize data in a structured way that is easy to read, modify, and manipulate. We can perform various operations on arrays, such as addition, subtraction, multiplication, and transpose. This knowledge can help you write efficient and effective programs that work with two-dimensional data structures.

With this tutorial, you should now have a solid understanding of two-dimensional arrays in C programming, and you can use this knowledge to take on more complex programming challenges in the future.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>