Arrays in C-Programming

<<Previous

Next >>





An array is a data structure that can store more than one value at a time. An array is a group of variables or collection of variables of a similar data type. In C, an array contain values called elements that are stored in contiguous memory locations. An array has a fixed size N and it is indexed from 0 to N-1. An array's element is accessed by using an index.

For example, if you want to store 100 data values, what will you do? We need to declare 100 individual variables. It will be very difficult to manage. Instead of declaring 100 variables, we will have to declare one single variable with length of 100. This single variable is referred as an array so that we can easily manage and access the individual variable.

In C, an array can be of one-dimensional and multidimensional.

Array Declaration

To declare regular variable, we only have to specify the data-type and variable name.

int mark;

To declare an array, we have to specify data-type, array name and square brackets which indicate the maximum number of elements. The following are the general syntax of the array declaration.

data-typename  array-name[ index-size];

Notice that there is no space between [ and name of the array.

Example

	int mark[5];

This statement would cause contiguous space for five integer type variables in the memory. This declaration is called one-dimensional array. These five integers are called elements of an array and that are referred in a program by writing:

	mark[0]
	mark[1]
	mark[2]
	mark[3]
	mark[4]

Therefore, An array can be declared by specifying the data-type, name of the array and require number of elements.

Array Initialization

There are two ways in which an array is initialized. It can be done either at the time of declaration or each element separately.

	int  mark[5] = { 30,40,50,78,56};
The above array can also be written as shown below:
	int mark[5];
	mark[0] = 30;
	mark[1] = 40;
	mark[2] = 50;
	mark[3] = 78;
	mark[4] = 56;

Suppose an array is initialized in a single line. In this case, there is no need to specify the size in the square brackets, the array will automatically be allocated memory which matches the number of elements in the list. The above statement will also be written as

	 //integer array in which each element is separated by comma.
	 int mark[ ] = { 30,40,50,78,56};

If the number of values in the initialiser list is less than the size of the array, the remaining elements of the array are initialized to zero.

The size of an array may be calculated by using sizeof operator.

Example

	// size will be assigned a value depending on the size of the array mark.
	//In this case, it is 5.
	int size=sizeof(mark);

In case of character array, it can be initialized by specifying list of characters enclosed with single quotes within the brace{ }. Here each character is separated by using comma.

	//character array
	char name[ ] ={'H','E','L','L','O'};

This may also be initialized by using string constant enclosed by double quotes.

	char name[ ] = "HELLO";

The string initialization automatically appends a "\0" character, so the above array is of size 6 not 5. It is equivalent to writing,

	char name[ ] 'H', 'E', 'L', 'L', 'O', '\0'}; //character array

an array element is accessed by using index value. As mentioned earlier, an array index starts from 0 and ends at arraySize-1.

From the above example
	int number = mark[3];

which means, the 4th element of mark array will be assigned to the variable number. So the value of number = 78.

Following is a program to display integer array with their address.

#include<stdio.h>
void main()
{
	int a[5]={1,2,3,5,6};
	int i;
	for(i=0;i < 5;i++)
	{
		printf("\n The value and address is:%d \t\t[%u]",a[i],&a[i]);
	}
}

Output


 The value and address is:1             [4294953920]
 The value and address is:2             [4294953924]
 The value and address is:3             [4294953928]
 The value and address is:5             [4294953932]
 The value and address is:6             [4294953936]
Please note that the addresses may vary from machine to machine and time to time

<< Previous

Next >>