ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

2D Array in C Programming Language

Updated on May 28, 2012
Source

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 a conceptual structure of 2D arrays. This 2D array image will help you to access any value stored in array.
This image is just a conceptual structure of 2D arrays. This 2D array image will help you to access any value stored in array. | Source

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. | Source

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.

Source

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?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      usha 3 years ago

      WAP which input 2 integer in 2D array and find sum of odd integer?

    • profile image

      amsareka 4 years ago

      understandable explanation keep it up and thanks

    • profile image

      k.sinduja reddy 5 years ago

      k.not bad

    • profile image

      gajanan 5 years ago

      super......

    • profile image

      NAveed Cheema 5 years ago

      your code is not correct

    • profile image

      rima 6 years ago

      aaaaaa shutttte

    • profile image

      hhh...d 6 years ago

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

    • profile image

      plz gve more examples in practical like using students 6 years ago

      very useful

    • profile image

      sanju 6 years ago

      write program of maximum and minimum using twodemension array using c

    • profile image

      pmuralimohan 6 years ago

      nice,very good thank you

    • profile image

      6 years ago

      super

    • profile image

      Dhaval 6 years 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?????

    • profile image

      madelyn 6 years ago

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

    • profile image

      zoya 6 years ago

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

    • profile image

      HIMA 6 years ago

      GOOD

    • profile image

      Rahul 6 years ago

      it is awesome

    • profile image

      Sunil 6 years ago

      C is very easy to learn

    • profile image

      mamatha.k 6 years ago

      not bad

    • profile image

      sivaramakash 6 years ago

      excellent

    • profile image

      sreeji 7 years 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?

    • profile image

      YAQOOB 7 years ago

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

    • profile image

      Nisha 7 years ago

      Thank you friend for written data type.

    • profile image

      pavan 7 years ago

      i want merging two dimensional arrays in c language

    • profile image

      selvi.k 7 years ago

      hi frnds i got some idea about arrays after seeing tat

    • profile image

      Parvez 7 years ago

      Nice...Thank you.

    • profile image

      amrik 7 years ago

      Way too much ads crap. Impossible to read normally.

    • profile image

      Meena 7 years ago

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

    • profile image

      ryan 8 years ago

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

    • profile image

      kashya[ 8 years ago

      very good site

      i got my all problems clear

    • profile image

      bucles 8 years ago

      nice-1!

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: "https://hubpages.com/privacy-policy#gdpr"

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)