Array in C – Declare Initialize Access and Operations

Here We learn Array in C how to Declare Initialize and access array and operations on array.

Array is a data structure that can store elements of the same type.

Array is a collection of similar type elements

It stores the elements in a contiguous memory locations.

1 Why Array Required?

Array is used to store similar types of values under same name

 We want to declare 100 integer variable

Instead of declaring 100 individual variables, such as

int number0, number1,number2,number3, ... …., number99;

you can declare one integer array variable such as

int number[100];

here number[100] is an integer array of size 100, means this array can store 100 integer value.

Array indexing starts from index 0 to n-1. Means first integer number store in array number[0], second integer number store in array number[1], third integer number store in array number[2], and so on. Here 100th  number store in array numbers[99].

2 How to Declare an Array in C?

Syntax of declare an array in C is as below

DataType  arrayName [ arraySize ];

This is called a single-dimensional array.

DataType : any valid C data type like int, char, float, double, long
array Name: Any valid c identifier
arraySize: an integer constant greater than zero.

For Instance

  1. int age[5];
  2. long length[8];
  3. float price[7];
  4. double value[5];
  5. char ch[5];

Above all are example of array.

  1. age is a integer array of size 5.
  2. length is a long array of size 8
  3. price is a float array of size 7
  4. value is double array of size 5
  5. ch is a character array of size 5

Size tells us that a array can store maximum element up to size.

Size must be a integer constant. based on size array occupies memory location for elements.

There are multi dimensional array.

3 Array Initialization

Giving initial value to array is know as array initialization.

A Initializing each member

We can initialize each element one by one as below.

int age[5]; //declaration

age[0]=21;
age[1]=24;
age[2]=25;
age[3]=27;
age[4]=26;

Array uses zero based indexing so first element is stored in age[0] and last element will store in size-1 position here age[4];

B Declare and initialize

general syntax is

DataType arrayName[size]={value1,value2,.............,valueN};

example

int age[5]={21,24,25,27,26};

value 21 will store in age[0] and value 24 will store in age[1] and similar for other values.

While initializing array giving size is optional;

like

float price[]={22.2,44.2,67.33,55.66,43.2};

Some times we want to initialize only few elements of array in that case we have to provide size of array and initialize only few values other values are initialized by zero.

double value[20]={10.0,21.0};

Here from value[3] to value[19] will be initialize by 0.0

4 Access Array Elements

Array uses zero based indexing to access elements.

to access element of an array, we can use syntax

arrayName[index];

int age[5]={21,24,25,27,26};

age[0], age[1],age[2],age[3],age[4], will access 21,24,25,27 and 26 respectively.

Lets see few examples to store values in array and access array values.

Example: write a program to create integer array and store 5 integer number and print.  

Description : In the above program,   int  num[5] array can store 5 element in a contiguous memory locations from index 0 to 4.         

Example: write a program to take 5 number from user and store integer number in integer array and print.  

OUTPUT

Example: Print the sum of all elements of an array

Output

5 Operations on Array

On array we can perform following basic operations

  1. Traverse– accessing all element of array
  2. Insertion– adding a new element at specified position
  3. Update – changing the existing value at specified position
  4. Deletion– deleting an element from specified position
  5. Search– search a element in array

A Traverse an array

Traversing is accessing the element of array.

Here we will access each element and print it

Output

B. Inserting an element in Array

add a new element in array can be done at various position

  • Insertion at the beginning of array
  • add at the give index position
  • Insert at last

Here we will insert a element based on index position.

On inserting a new element at specified position next element will move one space forward in array.

Output

C Update an element in Array

To update an array element we have to select element based on index value and provide new value to replace the existing one.

Simple program to update array value based on index is as below

Output

D Delete an element in Array

To delete an array element First select index value of element.

After deleting array element other elements are shifted forward to fill the gap of deleted element.

Output

E Search an element in Array

Search operation is used to find an element in array.

If element fond return index and element value.

If not it will return -1.

Output

F Sorting elements in Array

Arranging array elements in ascending or in descending order is known as sorting of array.

Following program is used to sort array elements in ascending order.

Output