Friday 26 December 2014

C - ARRAY

C Array is a collection of homogeneous value i.e. belongings to the same data type. You can store group of data of same data type in an array.
  • Array might be belonging to any of the data types.
  • Array size must be a constant value.
  • Always, Contiguous (adjacent) memory locations are used to store array elements in memory.
  • It is a best practice to initialize an array to zero or null while declaring, if we don’t assign any values to array. 
For example: If the user want to store marks of 100 students. This can be done by creating 100 variable individually but, this process is rather tedious and impracticable. These type of problem can be handled in C programming using arrays.

There are 3 types of C arrays. They are,
  1. One dimensional array
  2. Two dimensional array
  3.  Multidimensional array
One Dimensional Array:

Example:
#include
int main()
{
    int i;
    int arr[5] = {10,20,30,40,50};  
    // declaring and Initializing array in C
    //To initialize all array elements to 0, use int arr[5]={0};
    /* Above array can be initialized as below also
       arr[0] = 10;
       arr[1] = 20;
       arr[2] = 30;
       arr[3] = 40;
       arr[4] = 50;
    */
    for (i=0; i<5; i++)
    {
        //Accessing each variable
        printf("value of arr[%d] is %d \n", i, arr[i]);
    }
}
Output:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50

Notes:
1.If number of values in list is less than the number of elements or size of array then only that many element will be initialized.The remaining elements will be set to zero automatically. If array is character type then remaining element will be NULL.
Ex:
        int a[5]={3,4,5};
        value of arr[0] is 3
        value of arr[1] is 4
        value of arr[2] is 5
        value of arr[3] is 0
        value of arr[4] is 0
2.The size may be omitted. In such case, the compiler allocates enough space for all initialized elements.
Ex:
        int counter[]= {1,1,1,1};
        It will works fine.

Two Dimensional Array: 

Example:
#include
int main()
{
    int i,j;
    // declaring and Initializing array
    int arr[2][2] = {10,20,30,40};
    /* Above array can be initialized as below also
       arr[0][0] = 10;   // Initializing array
       arr[0][1] = 20;
       arr[1][0] = 30;
       arr[1][1] = 40;
    */
    for (j=0;j<2;j++)
       {
          // Accessing variables
          printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
       }
    }
}

Output:
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40

Note:
1.Some different types initialization:
int table[2][3] = {0,0,0,1,1,1};
             It simply initialized first row with 0 and second row with 1.
int table[2][3] = {{0,0,0},{1,1,1}};
             In this only we specify curly braces to distinguish the rows.So it gives same result.
int table[2][3] ={
                            {0,0,0},
                             {1,1,1}
                           };
             It also same as above.Commas are required after each braces that closes off a row, except in the case of the last row.
int table[][3] = {{0,0,0},{1,1,1}};
            When the array is completely initialized with all values, explicitly, we need not specify the  size of the first dimension.
int table[2][3] = {{1,1},{2}};
            If values are missing in an initializer, they are automatically set to zero. In above example,it initialize first two element of the first row to 1, the first element of the second row to two, and all other elements to zero.
int table[2][3] ={{0},{0}};
            In above statement, all elements are to be initialized to zero.

Note: We discussed later about Multidimensional array after discussing about Pointer.

0 comments :

Post a Comment