Wednesday 20 August 2014

Filled Under: ,

Array in C

Share
Array is a type of variable in c which is use to store the more than one value but is similar kinds. In fact  C Array is a collection of variables belongings to the same data type. You can store group of data of same data type in an array.
  • Array might be belonging to any of the data types
  • Array size must be a constant value.
Always, Contiguous (adjacent) memory locations are used to store array elements in memory.
It is a best practice to initialize an array to zero or null while declaring, if we don’t assign any values to array.

Example for C Arrays:
int a[10]; // integer array
char b[10]; // character array i.e. string
Types of C arrays:
There are two types of C arrays. They are,
  • One dimensional array
  • Multi dimensional array
Two dimensional array
Three dimensional array, four dimensional array etc… may be the types of array.
One dimensional array in C:
Syntax : data-type arr_name[array_size];
Array declaration
data_type arr_name [arr_size];
Array initialization
data_type arr_name [arr_size]=(value1, value2, value3,….);
Accessing array
arr_name[index];
Example :
int age [5]; int age[5]={0, 1, 2, 3, 4, 5}; age[0];_/*0_is_accessed*/
age[1];_/*1_is_accessed*/
age[2];_/*2_is_accessed*/
char str[10]; char str[10]={‘H’,‘a’,‘i’}; (or)
char str[0] = ‘H’;
char str[1] = ‘a’;
char str[2] = ‘i; str[0];_/*H is accessed*/
str[1]; /*a is accessed*/
str[2]; /* i is accessed*/
Example program for one dimensional array in C:
#include<stdio.h>
int main()
{
 int i;
 int arr[5] = {10,20,30,40,50}; 
 // declaring and Initializing array in C
 //To initialize all array elements to 0, use int arr[5]={0};
 /* Above array can be initialized as below also 
 arr[0] = 10;
 arr[1] = 20;
 arr[2] = 30;
 arr[3] = 40;
 arr[4] = 50; 
 */
 for (i=0;i<5;i++)
 {
 // Accessing each variable
 printf("value of arr[%d] is %d n", i, arr[i]);
 }
}
Output:
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
Two dimensional array in C:
Two dimensional array is nothing but array of array.
syntax : data_type array_name[num_of_rows][num_of_column]

Array declaration
data_type arr_name [num_of_rows][num_of_column];
Array initialization
data_type arr_name[2][2] = {{0,0},{0,1},{1,0},{1,1}};
Accessing array
arr_name[index];
Example:
int arr[2][2]; int arr[2][2] = {1,2, 3, 4}; arr [0] [0] = 1;
arr [0] ]1] = 2;
arr [1][0] = 3;
arr [1] [1] = 4;
Example program for two dimensional array in C:
#include<stdio.h>
int main()
{
 int i,j;
 // declaring and Initializing array
 int arr[2][2] = {10,20,30,40};
 /* Above array can be initialized as below also
 arr[0][0] = 10; // Initializing array
 arr[0][1] = 20;
 arr[1][0] = 30;
 arr[1][1] = 40; 
 */
 for (i=0;i<2;i++)
 {
 for (j=0;j<2;j++)
 {
 // Accessing variables
 printf("value of arr[%d] [%d] : %dn",i,j,arr[i][j]);
 }
 }
}
Output :
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40