Total Pageviews

Friday, 4 May 2012

Single and Multi-dimensional Arrays


Let us first look at how we define arrays in C:

  int listofnumbers[50];

BEWARE: In C Array subscripts start at 0 and end one less than the array size. For example, in the above case valid subscripts range from 0 to 49. This is a BIG difference between C and other languages and does require a bit of practice to get in the right frame of mind.

Elements can be accessed in the following ways:-

               thirdnumber=listofnumbers[2];
               listofnumbers[5]=100;

Multi-dimensional arrays can be defined as follows:

               int tableofnumbers[50][50];
    for two dimensions.

For further dimensions simply add more [ ]:

              int bigD[50][50][40][30]......[50];

Elements can be accessed in the following ways:

               anumber=tableofnumbers[2][3];
               tableofnumbers[25][16]=100;

No comments:

Post a Comment