2.14.2013

Array in C program

 Array Declaration 

To declare an array we need three things as:
  1. What kind types(i.e. data type) of array?
  2. What is the name of array?
  3. What is the size of array?
In our C program, we have declare array as:

syntax: data_type array_name[SIZE];
example: int result[10];

Here, int is a data type and the word result is the name of array. The [10] is however is new. The number 10 tells how many elements of the type int will be in our array. This number is often called the 'dimension' of the array. The bracket ( [] ) tells the compiler that we are dealing with an array.

 Accessing element of an Array 

Once an array is declared, we can access it and use it in our program.
Now you know that, all the array elements are numbered, starting with 0. Thus, result[10] is not the 10th elements of the array, but the 9th.
We are using the variable i as a subscript to refer to various elements of the array. This variable can take different values and hence can refer to the different elements in the array in turn. This ability to use variables to represent subscripts is what makes arrays so useful.

 Entering Data into an Array 

int result[10];
for(i=0; i<10; i++)
{
 printf("\nEnter elements: ");
 scanf("%d", &result[i]);
}


The for loop causes the process of asking for and receiving a result from the user to be repeated 10 times. The first time through the loop, i has a value 0, so the scanf() functions will cause the value typed to be stored in the array element marks[0], the first element of the array. This process will be repeated until i become 9. This is last time through the loop, which is a good thing, because there is no array element like result[10].

 Reading data from an array 

for(i=0; i<10; i++)
  printf("\n%d",result[i]);

 Array Initialisation 

We has been discuss about how to declare arrays, access them and read them. Let us now see how to initialize an array while declaring it as:

syntax:
data_type arrayName[SIZE]={n1,n2,n3,.....}
Here, n1,n2,... etc are equal to SIZE.

example:
int result[5]={10,20,4,8,99}
float price[]={1.5,53.3,-66.6}
int race[]={11,4,579,31,278,3}

 Till the array elements are not given any specific values, they are supposed to contain garbage values.
 If the array is initialised where it is declared, mentioning the dimension of the array is optional as in the 2nd and 3rd example above.

 Array elements in memory 

Consider the following array declaration:

int arr[5];

What happens in memory when we make this declaration?
10 bytes get immediately reserved in memory, 2 bytes each for the 5 integers. And since the array is not being initialized, all five values present in it would be garbage values. This so happens because the storage class of this array is assumed to be auto. If the storage class is declared to be static then all the array elements would have a default initial value as zero. Whatever be the initial values, all the array elements would always be present in contiguous memory locations. This arrangement of array elements in memory is shown in the following figure as:

Memory representation of array
Figure: Array in memory map demonstration

Related article:

1 comment:

  1. In this example if we print 6th element of an array it will show a garbage value

    ReplyDelete