Array and it's Type by G Krishna Chauhan

ARRAY : 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.

It a kind of data structure that can store a fixed-size sequential collection of elements of the same type. 
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Array might be belonging to any of the data types.

Instead of declaring individual variables, such as number0, number1, ..., and number99.
you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables.
A specific element in an array is accessed by an index.





Array size must be a constant value. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
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.



TYPES OF C ARRAYS: There are 2 types of C arrays. They are,

  1. One dimensional array
  2. Multi dimensional array 

EXAMPLE FOR C ARRAYS:
int a[10];       // integer array
char b[10];    // character array   i.e. string


Array declaration syntax:
data_type arr_name [arr_size];

Array initialization syntax:
data_type arr_name [arr_size]=(value1, value2, value3,….);

Array accessing syntax:
arr_name[index];

Integer array example:
int age [5];
int age[5]={0, 1, 2, 3, 4};

age[0]; /*0 location is accessed*/
age[1]; /*1 location is accessed*/
age[2]; /*2 location is accessed*/

Character array example:
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 location is accessed*/
str[1]; /*a location is accessed*/

str[2]; /*i location is accessed*/

Initializing Arrays:
You can initialize an array in C either one by one or using a single statement as follows −

double bal[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write −

double bal[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
You will create exactly the same array as you did in the previous example. Following is an example to assign a single element of the array −

bal[4] = 50.0;

The above statement assigns the 5th element in the array with a value of 50.0. All arrays have 0 as the index of their first element which is also called the base index and the last index of an array will be total size of the array minus 1. 

Example Program :

#include<stdio.h>
#include<conio.h>
 void main()
{
   int i;
   int arr[5] = {110,320,340,450,520}; // This is 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] = 110;
      arr[1] = 320;
      arr[2] = 340;
      arr[3] = 450;
      arr[4] = 520; */
   for (i=0;i<5;i++)
   {
      // Accessing each variable
      printf("value of arr[%d] is %d \n", i, arr[i]);
   }
 }


Click here for 1 Dimention Array

No comments:

Post a Comment