BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Two dimensional (2D) arrays in C programming with example

Last Updated: July 25, 2022 by Chaitanya Singh | Filed Under: c-programming

An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns.

Let’s take a look at the following C program, before we discuss more about two Dimensional array.

Simple Two dimensional(2D) Array Example

This program demonstrates how to store the elements entered by user in a 2d array and how to display the elements of a two dimensional array. For now don’t worry about the initialization of two dimensional array shown in this example, we will discuss that part later.

#include<stdio.h>
int main(){
   /* 2D array declaration*/
   int disp[2][3];
   /*Counter variables for the loop*/
   int i, j;
   for(i=0; i<2; i++) {
      for(j=0;j<3;j++) {
         printf("Enter value for disp[%d][%d]:", i, j);
         scanf("%d", &disp[i][j]);
      }
   }
   //Displaying array elements
   printf("Two Dimensional array elements:\n");
   for(i=0; i<2; i++) {
      for(j=0;j<3;j++) {
         printf("%d ", disp[i][j]);
         if(j==2){
            printf("\n");
         }
      }
   }
   return 0;
}

Output:

Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
1 2 3 
4 5 6 

Initialization of 2D Array

There are two ways to initialize a two Dimensional arrays during declaration.

int disp[2][4] = {
    {10, 11, 12, 13},
    {14, 15, 16, 17}
};

OR

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Although both the above declarations are valid, I recommend you to use the first method as it is more readable, because you can visualize the rows and columns of 2d array in this method.

Things that you must consider while initializing a 2D array

We already know, when we initialize a normal array (or you can say one dimensional array) during declaration, we need not to specify the size of it. However that’s not the case with 2D array, you must always specify the second dimension even if you are specifying elements during the declaration. Let’s understand this with the help of few examples –

/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }  
/* Valid declaration*/ 
int abc[][2] = {1, 2, 3 ,4 }  
/* Invalid declaration – you must specify second dimension*/
int abc[][] = {1, 2, 3 ,4 }   
/* Invalid because of the same reason  mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }

How to store user input data into 2D array

We can calculate how many elements a two dimensional array can have by using this formula: The array arr[n1][n2] can have n1*n2 elements. The array that we have in the example below is having the dimensions 5 and 4. These dimensions are known as subscripts. So this array has first subscript value as 5 and second subscript value as 4.
So the array abc[5][4] can have 5*4 = 20 elements.

To store the elements entered by user we are using two for loops, one of them is a nested loop. The outer loop runs from 0 to the (first subscript -1) and the inner for loop runs from 0 to the (second subscript -1). This way the the order in which user enters the elements would be abc[0][0], abc[0][1], abc[0][2]…so on.

#include<stdio.h>
int main(){
   /* 2D array declaration*/
   int abc[5][4];
   /*Counter variables for the loop*/
   int i, j;
   for(i=0; i<5; i++) {
      for(j=0;j<4;j++) {
         printf("Enter value for abc[%d][%d]:", i, j);
         scanf("%d", &abc[i][j]);
      }
   }
   return 0;
}

In above example, I have a 2D array abc of integer type. Conceptually you can visualize the above array like this:
2D-array

However the actual representation of this array in memory would be something like this:
memory-2D-diagram

Pointers & 2D array

As we know that the one dimensional array name works as a pointer to the base element (first element) of the array. However in the case 2D arrays the logic is slightly different. You can consider a 2D array as collection of several one dimensional arrays.

So abc[0] would have the address of first element of the first row (if we consider the above diagram number 1).
similarly abc[1] would have the address of the first element of the second row. To understand it better, lets write a C program –

#include <stdio.h>
int main()
{
   int abc[5][4] ={
            {0,1,2,3},
            {4,5,6,7},
            {8,9,10,11},
            {12,13,14,15},
            {16,17,18,19}
            };
    for (int i=0; i<=4; i++)
    {
        /* The correct way of displaying an address would be
         * printf("%p ",abc[i]); but for the demonstration
         * purpose I am displaying the address in int so that
         * you can relate the output with the diagram above that
         * shows how many bytes an int element uses and how they
         * are stored in contiguous memory locations.
         *
         */
    	printf("%d ",abc[i]);
    }
    return 0;
}

Output:

1600101376 1600101392 1600101408 1600101424 1600101440

The actual address representation should be in hex for which we use %p instead of %d, as mentioned in the comments. This is just to show that the elements are stored in contiguous memory locations. You can relate the output with the diagram above to see that the difference between these addresses is actually number of bytes consumed by the elements of that row.

The addresses shown in the output belongs to the first element of each row abc[0][0], abc[1][0], abc[2][0], abc[3][0] and abc[4][0].

How to calculate the address of an element in 2D array?

There are two techniques to find the address of an element in 2D array:
1. Row Major Order
2. Column Major Order

1. Row Major Order

If am array is represented by arr[x][y] where x represents the rows and y represents the columns then the address of a random element arr[i][j] can be calculated as follows:

Address of arr[i][j] = Base Address + (i * y + j) * size

Here Base Address represents the address of element arr[0][0] (the first element of the array).

Example: Given an array, arr[10][25] with base address 100 and the size of each element is 4 Bytes in memory. Find the address of arr[8][6] with the help of row-major order?

Address of arr[8][6] = Base Address + (8*25 + 6)*4
                     = 100 + (200+6)*4
                     = 100+824 = 924

2. Column Major Order

If an array is represented by arr[x][y] where x represents the rows and y represents the columns then the address of a random element arr[i][j] using column major method, can be calculated as follows:

Address of arr[i][j] = Base Address + (j * x + i) * size

Example: Given an array, arr[20][20] with base address value 100 and the size of each element is 4 Bytes in memory. Find the address of arr[5][16] with the help of column-major order?

Address of arr[5][16] = Base Address + (j * x + i) * size
                     = 100 + (16*20+ 5)*4
                     = 100+ 325*4
                     = 100+ 1300 = 1400
❮ PreviousNext ❯

Top Related Articles:

  1. C – Pointer to Pointer (Double Pointer) with example
  2. Passing array to function in C programming with example
  3. Structure in C programming with examples
  4. Pointer and Array in C programming with example
  5. C Preprocessor Directives

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. asritha says

    November 13, 2014 at 6:15 PM

    extend the topic on 2d arrays…sorting and searching

    Reply
    • ron weasely says

      September 10, 2015 at 12:01 PM

      there are two types
      1:linear search
      2:binery search
      you can search in wikipedia ,you will get extension about it.

      Reply
  2. ARSADHUSAIN says

    March 2, 2015 at 9:37 AM

    convert image into two dimension array plzzz help

    Reply
  3. shraddha says

    March 3, 2015 at 5:53 AM

    I want the code for Addition of arrays with output as array of nos. Please.

    Reply
    • Anonymous says

      April 3, 2016 at 3:39 PM

      #include
      #include
      #include

      int main()
      {
      int m, n, c, d, first[10][10], second[10][10], sum[10][10];
      clrscr();
      printf(“Enter the number of rows and columns of Array(2D)\n”);
      scanf(“%d%d”, &m, &n);
      printf(“Enter the elements of first Array\n”);

      for (c = 0; c < m; c++)
      for (d = 0; d < n; d++)
      scanf("%d", &first[c][d]);

      printf("Enter the elements of second Array\n");

      for (c = 0; c < m; c++)
      for (d = 0 ; d < n; d++)
      scanf("%d", &second[c][d]);

      printf("Sum of Arrays:-\n");

      for (c = 0; c < m; c++) {
      for (d = 0 ; d < n; d++) {
      sum[c][d] = first[c][d] + second[c][d];
      printf("%d\t", sum[c][d]);
      }
      printf("\n");
      }

      getch();
      }

      Reply
      • Elias Missibsa says

        April 16, 2016 at 12:22 PM

        Hey;

        very interesting web site.
        i enjoy it!

        thanks a lots!

        Reply
  4. Rajiur Rahman says

    March 4, 2015 at 7:00 PM

    How do I assign a 2-D array to a pointer ? I am getting error.

    Reply
  5. raztek says

    September 1, 2015 at 10:01 PM

    The figure label “2D array conceptual memory representation” has the explanation wrong because it switched the number of rows and columns.
    IT should say abc[5][4] , and
    There are 5 rows and 4 columns!!!

    Reply
  6. shubham teli says

    October 10, 2015 at 9:35 PM

    How to create an 2d array when I dont no row and column size & this value how to pass function.

    Reply
  7. aisha says

    December 2, 2016 at 6:58 AM

    create a dev c++ program where the user can insert fruits and their price and print its list.use 2 dimensional array

    help me for this please i really dont know how to do it

    Reply
  8. yuvasri says

    January 2, 2017 at 8:52 AM

    i need C program to print the address of particular element in two dimensional array

    Reply
  9. mohanteja says

    March 10, 2017 at 9:15 AM

    how to scan a 2d array in matrix way on console ?
    like
    enter elements
    2 3 6
    4 5 6
    1 2 3

    Reply
  10. VK says

    April 3, 2017 at 12:10 PM

    I need a program which stores a sentence in a 2D array. Can you help me with that?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap