C Language / Arrays

It is collection of variables / elements all are of same data type and all the variables / elements are referred using a single array name by index number. The variables / elements in an array are stored in consecutive memory locations. Array size says about number of elements in array. Arrays are of 2 types. They were

1. One dimensional arrays.
2. Multidimensional arrays.

One dimensional array Syntax
It consists of one index only.

<Data type>   <array name>[size/Index];

Examples Explanation
int a[10]; a = array name
a array size = 10 Elements
float b[20]; b = array name
b array size = 20 Elements
char c[15]; c = array name
c array size = 15 Elements

Initialization of one dimensional array
Giving values to array elements is called as Initialization of one dimensional array.

Example
int a[3]={10,20,30};

Program to add 3 numbers of an array. Output
#include<stdio.h>
void main()
{
int a[3]={10,20,30}, sum;
sum=a[0]+a[1]+a[2];
printf("\n Sum of 3 Numbers = %d",sum);
}
Sum of 3 Numbers =60

Explanation
a[0]=10
a[1]=20
a[2]=30
sum=a[0]+a[1]+a[2];
sum=10+20+30;
sum=60

#include<stdio.h>
void main()
{
int a[3], sum;
printf("\n Enter three Numbers :\n ");
scanf("%d%d%d",&a[0],&a[1],&a[2]);
sum=a[0]+a[1]+a[2];
printf("\n Sum of 3 Numbers = %d",sum);
}

Enter three Numbers : 10 20 30 Sum of 3 Numbers =60<

Passing array as argument to functions
Sample Program Output

#include<stdio.h>
int arrfun(int n);
void main()
{
int i;
int a[10]={1,2,3,4,5,6,7,8,9,10};
i=arrfun(a[3]);
if(i==0)
printf("Number is Even");
else
printf("Number is odd");
}

int arrfun(int n)
{
if(n%2==0)
return 0;
else
return 1;
}
Number is Even

//Adds all elements of an array

#include<stdio.h>
int arr_sum(int x[],int);
void main()
{
int a[5]={1,2,3,4,5},i;
printf("Sum is %d",arr_sum(a,5));
}

int arr_sum(int x[],int size)
{
int i,sum=0;
for(i=0;i<5;i++)
sum=sum+x[i];
return(sum);
}

Sum is 15

Multidimensional arrays
It consists of more than 1 index.

Two dimensional arrays Syntax
<Data type > <array name>[size1/Index1][size2/Index2];

Examples Explanation
int a[2][2]; a = array name
a array size = 2 * 2 = 4 Elements
int float b[2][3]; b = array name
b array size = 2*3=6 Elements
char c[3][4]; c = array name
c array size = 3 * 4 = 12 Elements

Initialization of two dimensional array Giving values to array elements is called as Initialization of one dimensional array.

Example
int a[2][2] = {1,2,3,4};
Array a

  1     2
  3   4  


int a[3][4] = {{1,2},{4,5,6,8},{7}}
//Array a

  1     2    0   0  
  4   5     6     8 
 7    0    0    0  


Sample Program to prints array values with their memory locations. Output

#include<stdio.h>
void main()
{
int a[2][2]={1,2,3,4};
int i,j;
//printing Element values
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
printf("%d",a[i][j]);
printf("\n");
}
//printing address of each location
for (i=0;i<2;i++)
{
for (j=0;j<3;j++)
printf("%u",&a[i][j]);
printf("\n");
}
}

12
34
166983644816698364521669836456
166983645616698364601669836464


Home     Back