Array in C Language

PREVIOUS                                                                                                                   NEXT



Array in C Language

Array is a collection elements of the same type stored in contiguous memory location. Array is divided into three categories.
   
      1. Single Dimensional Array
      2. Two Dimensional Array
      3. Multi Dimensional Array

We will see the details of the above dimensional array below

Single Dimensional Array

 The syntax of the single dimensional array is

data-type array-name[dimension]

The data type tells which type of data is stored in the array, the array name is the name of the array and the square bracket ([]) tells that the variable array-name is an array type variable and the dimension tells the range of the array, means how many elements can be stored in the array.

Example

int data[5];
This is the declaration of the array. The declaration tells the compiler that variable data is an array of integers of five elements.
Let us see an example how to store and access the each element of an array..

main()
{
    int arr[5];
    int count = 0;
    int i = 1;
    'storing data in a single dimensional array
    while(count++<=4)
    {
        arr[count]=i++;
     }
    'accessing data from a single dimensional array
    count=0;
    while(count++<=4)
    {
        printf("%d", arr[count] );
     }
}

Let us see another example

main()
{
    int arr[5];
    int count = 0;
    'storing data in a single dimensional array
    while(count++<=4)
    {
        prnitf("Enter an integer value ");
        scanf("%d",&arr[count]);
     }
    'accessing data from a single dimensional array
    count=0;
    while(count++<=4)
    {
        printf("%d", arr[count] );
     }
}


Array Initialization
In the above example we have enter the value in the array in the run time. But in the bellow program we will see how to initialization an array.
main()
{
    int arr[5]={1,2,3,4,5};
    int count = 0;
    'accessing data from a single dimensional array
    count=0;
    while(count++<=4)
    {
        printf("%d", arr[count] );
     }
}
Another example
main()
{
    int arr[]={1,2,3,4,5};
    int count = 0;
    'accessing data from a single dimensional array
    count=0;
    while(count++<=4)
    {
        printf("%d", arr[count] );
     }
}

In the above two program, in first one we explicitly give the range of the array as 5 and in the curly bracket if we wrongly enter more than 5 elements then it shows error. But in second example there is no explicitly array range is declared, here the range of the array is depending upon the elements present in the curly bracket. 

The memory allocation of a single dimensional array
In the above example when the compiler execute the statement
int arr[]={1,2,3,4,5};
or
int arr[5]={1,2,3,4,5};
the compiler tells that arr is an integer array of 5 element. In each element it stores an integer constant value and these 5 elements are stored in the contiguous memory. When the compiler executes the array declaration statement, it reserves, (the number of elements to be stored in the array x the size of the data type of the element), memory in the data segment.

    arr     
1
2
3
4
5


        1000                 1002                 1004                    1006                   1008
        arr[0]             arr[1]                arr[2]                     arr[3]                  arr[4]
Accessing array element by using array subscript

main()
{
   int arr[]={1,2,3,4,5};
   printf("value of first element is%d \n", arr[0]);
   printf("address of the first element is %d \n", &arr[0]);
   printf("value of second element is%d \n", arr[1]);
   printf("address of the first element is %d \n", &arr[1]);
}

The output of the program is
value of first element is 1
address of the first element is 1000
value of second element is 2
address of the second element is 1002

The declaration of other data types

    double arr[5];
        arr is a array of 5 element of double data type.
    float arr[5];
        arr is a array of 5 element of float data type.
    int* arr[5];
        arr is a array of 5 element of integer pointer type.

Let us see an example
main()
{
   int a=5;
   int b=6;
   int* arr[2];
   arr[0]=&a;
   arr[1]=&b;
   printf("value of first element of the array arr[0] is %u \n ", arr[0]);
   printf("address of the first element of the array arr[0] is %u \n ", &arr[0]);
   printf("value at the address arr[0] is %d \n ", *arr[0]);

   printf("value of second element of the arr[1] is%d \n", arr[1]);
   printf("address of the first element of the arr[1] is %d \n", &arr[1]);
   printf("value at the address arr[1] is %d \n", *arr[1]); 
}

array of pointers representation

 The output of the program is

   value of first element of the array arr[0] is 1250
   address of the first element of the array arr[0] is 300
   value at the address arr[0] is 5

   value of second element of the arr[1] is 6210
   address of the first element of the arr[1] is 302
   value at the address arr[1] is 6

When we access the arr[0], then like ordinary array it gives the data stored in it, here the data is the address of a integer variable i.e 1250. Here we use %u because the address is a unsigned integer variable. When we access the address of the arr[0] ( &arr[0] ) it gives the address of the first element of the array arr i.e 300 where the arr variable is stored in the data segment. when we access *arr[0], reads as value stored at the address arr[0], i.e value stored at the address 1250, i.e 5.

    *arr[0]
= *(arr[0])
= *(1250)
= 5

Pointer to an single dimensional array

pointer to an integer array

int a[5] = {1,2,3,4,5};
int(*p)[5];
p=&a;


Note

    1. Array name gives the address of the first element of the array.
    2. &a gives the address of the entire array.
    3. In pointer to an array declaration statement, the array subscript value must be same as the array element to which it is pointing.
    4. An array subscript value must be an integer constant.

The difference between following declarations

 1. int* arr[5];
 2. int (*arr)[5];
 3. int *(arr3[5]);


int* arr[5];
    arr is an array of pointers to integer variable of five element.
int *(arr3[5]);
    same as above,arr is an array of pointers to integer variable of five element.
int (*arr)[5];
    arr is a pointer to an integer array of five element.

Eample

main()
{
  int a[]=(1,2,3,4,5};
  int(*arr)[5];
  arr = &a;
  //arr = a;
  printf("Address of the 1st element of the array a = %u \n",a);
  printf("Address of the 1st element of the array &a[0] = %u \n",&a[0]); 
  printf("Address of the entire array &a = %u \n",&a);
  printf("Address of the entire array arr = %u \n",arr); 
  printf("Address of the 2nd element of the array a+1 = %u \n",a+1); 
  printf(" &a+1 = %u \n",&a+1);  
  printf("arr+1 = %u \n",arr+1);
}

The output of the program is

Address of the 1st element of the array a = 1000
Address of the 1st element of the array &a[0] = 1000 
Address of the entire array &a = 1000
Address of the entire array arr = 1000
Address of the 2nd element of the array a+1 = 1002
&a+1 = 1010  
arr+1 = 1010 
 
Note 
arr = a;
Here if we execute the statement then a compiler time error comes. Because a gives the address of first element of the array i.e. an address of an integer. And arr is a pointer to an integer array of five elements. So there is a type mismatch. So the error comes. 

PREVIOUS                                               NEXT

No comments:

Post a Comment