Introduction to C arrays

Array Introduction

Array is a collaction of the simillar data type. An array is also a variable , it must be declared and defined before it can be used.

Benefits of array-> An ordinary variable can only contain a single value but array can store multiple variable of the simmilar datatype.

Values are store in the consecutive memory location.

There are two tpyes of array

  1. One-dimentional array
  2. Multi-dimentional array

syntax

data_type        variable_name[SIZE];     /* Here SIZE = Number of element*/

int                    arr[10];     /* Declaration of array arr */

Here we say arr is an 10 element integer array.

float                 arr[10];

Here we say arr is an 10 element float array.

Declaring multple array at same time and of same type

int        arr[10], arr1[20];

 

Initialization of one-dimentional array

array can be intialized during declaration time

int        arr[5]={10,20,30,40,50};

Some rules to remember when initializing during declaration

  •  If the list of initial elements is shorter than the number of array elements, the remaining elements are initialized to zero.
  •  If a static array is not initialized at declaration manually, its elements are

automatically initialized to zero.

  • If a static array is declared without a size specification, its size equals the

length of the initialization list.

when we intialize during declaration time then giving array size is not neccessary like

int        arr[]={10,20,30,40,50}; /*  Here size of arr is 5 */

Accessing array

here we come to know how to access an array element

int arr[]={10,20,30,40,50}; /* Here i want to access value ’30’ */

array name followed by position i.e index

format ->                     arr[position]

arr[0] = 10;

arr[1] = 20;

arr[2] = 30

arr[3] = 40;

arr[4] = 50;

So position of 30 is at index ‘2’ starting from index ‘0’. If you want to change value at index ‘2’

arr[2] = 100;     /* Now new array list is given below */

arr[] = {10,20,100,40,50};

Or you want to replace value at index ‘0’ by value at index ‘4’.

arr[0] = arr[4];     /* Now new array list is given below */

arr[] = {50,20,100,40,50};

A small program to check

#inckude<stdio.h>

void main()

{

            int arr[5] = {50,20,100,40,50};

            int i;

            for (i = 0; i<5; i++)

            printf(“value at index[%d] = %d\n”,i,arr[i]);

            scanf(“%d”,&arr[0]); /* New value entered from ketboard is 25 */

            /* Again print values */

            for (i = 0; i<5; i++)

            printf(“value at index[%d] = %d”,i,arr[i]);

}

Output:-

value at index[0] = 50

value at index[1] = 20

value at index[2] = 100

value at index[3] = 40

value at index[4] = 50

25

value at index[0] = 25

value at index[1] = 20

value at index[2] = 100

value at index[3] = 40

value at index[4] = 50

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.