Flag This Hub

2 Dimensional Array in C Programming Language

By


We know how to work with an array (1D array) having one dimension. In C language it is possible to have more than one dimension in an array. In this tutorial we are going to learn how we can use two dimensional arrays (2D arrays) to store values. Because it is a 2D array so its structure will be different from one dimension array (1D array). The 2D array is also known as Matrix or Table, it is an array of array. See the 2D array image, in that image each row is an array.

Declaration of 2D array:

Syntax: data_type array_name[row_size][column_size];
Example: int arr[3][3];
So the above example declares a 2D array of integer type. This integer array has been named arr and it can hold up to 9 elements (3 rows x 3 columns).

2D array image

This image is just an conceptual structure of 2D arrays. This 2D array image will help you to access any value stored in array.
See all 3 photos
This image is just an conceptual structure of 2D arrays. This 2D array image will help you to access any value stored in array.

Memory Map of 2D Array

This image illustrates how 2D array is actually stored in memory.
This image illustrates how 2D array is actually stored in memory.
C Programming Language (2nd Edition)
Amazon Price: $35.64
List Price: $67.00
C (Vintage)
Amazon Price: $1.03
List Price: $15.00
Programming in C (3rd Edition)
Amazon Price: $24.00
List Price: $49.99
C Primer Plus (5th Edition)
Amazon Price: $30.63
List Price: $54.99
Absolute Beginner's Guide to C (2nd Edition)
Amazon Price: $19.00
List Price: $39.99
Data Structures In C
Amazon Price: $23.94
List Price: $30.00
Data Structures and Algorithms Made Easy: Data Structure and Algorithmic Puzzles, Second Edition
Amazon Price: $31.38
List Price: $39.99
Data Structures and Algorithms in Java (2nd Edition)
Amazon Price: $32.00
List Price: $64.99
Data Structures and Algorithms Made Easy: Data Structure and Algorithmic Puzzles
Amazon Price: $30.94
List Price: $39.99
Data Structures and Algorithms
Amazon Price: $49.00
List Price: $74.20

Code for assigning & displaying 2D Array

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j;
int arr[3][3]={
		{12, 45, 63},
		{89, 34, 73},
		{19, 76, 49}
		};
clrscr();
printf(":::2D Array Elements:::\n\n");
for(i=0;i<3;i++)
{
	for(j=0;j<3;j++)
	{
	printf("%d\t",arr[i][j]);
	}
	printf("\n");
}
getch();
}

Displaying 2D array using for loop.

So in the above example we have declared a 2D array named arr which can hold 3x3 elements. We have also initialized that array with values, because we told the compiler that this array will contain 3 rows (0 to 2) so we divided elements accordingly. Elements for column have been differentiated by a comma (,). When compiler finds comma in array elements then it assumes comma as beginning of next element value. We can also define the same array in other ways, like.
int arr[3][3]={12, 45, 63, 89, 34, 73, 19, 76, 49}; or,
int arr[ ][3]={12, 45, 63, 89, 34, 73, 19, 76, 49};

But this kind of declaration is not acceptable in C language programming.

int arr[2][ ]={12, 45, 63, 89, 34, 73, 19, 76, 49}; or,
int arr[ ][ ]={12, 45, 63, 89, 34, 73, 19, 76, 49};

To display 2D array elements we have to just point out which element value we want to display. In our example we have a arr[3][3], so the array element reference will be from arr[0][0] to arr[2][2]. We can print display any element from this range. But in our example I have used for loop for my convenience, otherwise I had to write 9 printf statements to display all elements of array. So for loop i handles row of 2D array and for loop j handles column. I have formatted the output display of array so that we can see the elements in tabular form.

Visitor's Opinion

Is this tutorial on 2D-Array clear enough to understand?

  • Yes, this tutorial clearly explained 2D-Array.
  • No, more examples needed on 2D- Array
See results without voting

bucles 2 years ago

nice-1!

kashya[ 2 years ago

very good site

i got my all problems clear

ryan 2 years ago

thanks... i can use it on my class...

Meena 20 months ago

I have clear idea when i was seen this site. Thank you

amrik 20 months ago

Way too much ads crap. Impossible to read normally.

Parvez 19 months ago

Nice...Thank you.

selvi.k 17 months ago

hi frnds i got some idea about arrays after seeing tat

pavan 17 months ago

i want merging two dimensional arrays in c language

Nisha 16 months ago

Thank you friend for written data type.

YAQOOB 15 months ago

yesterday i was completely confuse when i was writing 2d array prog but now now my concept is totally clear about it

sreeji 14 months ago

is there any difference between these two assignment?

int arr[ ][3]={12, 45, 63, 89, 34, 73, 19, 76, 49};

and

int arr[3 ][]={12, 45, 63, 89, 34, 73, 19, 76, 49};

compiler shows second one is wrong..y?

sivaramakash 9 months ago

excellent

mamatha.k 8 months ago

not bad

Sunil 8 months ago

C is very easy to learn

Rahul 7 months ago

it is awesome

HIMA 7 months ago

GOOD

zoya 7 months ago

its not enough,,abut array more explanation shud be there,,

madelyn 7 months ago

it is not easy making array statement without following the example given by the instructor or else the teacher

Dhaval 7 months ago

Write a program that creates a two-dimensional array with 10 rows and 2 columns. The first column should be filled with 10 random numbers between 0 and 100. The second column should contain the squared value of the element found in column 1. Using the show ( ) method of the MessageBox class, display a table. Please someone help me out on this program?????

6 months ago

super

pmuralimohan 5 months ago

nice,very good thank you

sanju 3 months ago

write program of maximum and minimum using twodemension array using c

plz gve more examples in practical like using students 3 months ago

very useful

hhh...d 2 months ago

write aprogram read the integar 10 elements into an array and display it reverse order?

rima 4 weeks ago

aaaaaa shutttte

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working