2 Dimensional Array in C

2D Array

2D Array is defined as the continuous block of memory in which a similar data type is stored. 2D Array indexing starts with size 0 and ends with size -1. The size of the 2D array is fixed and it is stored in static memory.

2 Dimensional Array in C

Syntax:

 data_type array_name [i][j]

Representation of 2D Array

Array Declaration

Declaration of 2D Array consists of any data type and it can be declared by any random name or any random variable.

Syntax:

int arr[3][5];

// Here int is the data type of array.
// arr is the name of array.
// 3 is the size of row and 5 is the size of column of an array.

Array Initialization

In 2D Array, All the elements consist of garbage value at initial time but it can be explicitly initialized during declaration.

Syntax:

 int Arr[i][j] = {value 1, value 2, value 3,…,value ij}

Accessing of elements in 2D Array:

In 2D Array elements are accessed by putting the array name and index value inside square brackets.

Syntax:

<arr_name>[index];

Rules for 2D Array Declaration:

  • In 2D array we must have to declare the array variable.
  • 2D Array stores the data in contiguous memory locations.
  • 2D Array  should be a list of similar data types.
  • The size of the 2D array should be fixed.
  • 2D Array should be stored in static memory.
  • 2D Array indexing starts with size 0 and ends with size -1.
  • In 2D Array declaration of array size within the bracket is not necessary.
  • In 2D Array it must include the variable name  and data type while declaring any arrays .

Example 1

Run

#include <stdio.h>
int main() {
    int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", *(*(arr + i) + j)); 
        }
        printf("\n");
    }
    return 0;
}

Output:

1  2  3
4  5  6
7  8  9

Example 2

Run
#include
int main() {
    int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    printf("The even elements in the 2d array are : ");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if(arr[i][j]%2==0) printf(" %d ", *(*(arr + i) + j));
        }
    }
    return 0;
}

Output

The even elements in the 2d array are : 2 4 6 8 

Example 3

Run

#include <stdio.h>
int main() {
    int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if(i==0 && j==0){
            printf("The first element of 2d array is : %d\n ", *(*(arr + i) + j)); 
            }
            else if(i==2 && j==2){
            printf("The last element of 2d array is : %d ", *(*(arr + i) + j)); 
            }
        }

    }
    return 0;
}

Output

The first element of 2d array is : 1
The last element of 2d array is : 9

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription