Array in C Programming

Array:-

An array is a list of finite number of elements of same data type i.e, integers, strings etc.
Syntax of array declaration is as follows:
Elements of an array cannot have different types of data.  Array elements in computer memory are stored in a sequence of contiguous memory blocks.  All the elements in an array are of the same data type, so only the memory blocks that are allocated for the elements of the array are also of the same size.
Array

Every element of an array occupies a memory block.  Now what will be the size of this memory block, it depends on the data type.  The shortest address means 0 in the array is used to access the first element and the largest address high element.

Operations on array:-

The operations performed on linear structure i.e. arrays are:
  • Traversal: Processing each element in the array or list.
  • Search: Finding the location of the element with a given value or widh a given key.
  • Insertion: Adding a new element to the list.
  • Deletion: Removing an element from the list. 
  • Sorting: Arranging the element in some type of order, ascending or descending.
  • Merging: Combining two lists or arrays within a single list or array initialization.

Type Of Array:-

There is three types of array in C programming language:
Type of array
  • One-Dimensional Arrays
  • Two-Dimensional Arrays
  • Multi-Dimensional Arrays

1. One-Dimensional Arrays:-

A one-dimensional array is a type of linear array. 
Read Also:
  1. First C Program
  2. FlowChart
  3. Operators
A list of items can be given one variable name using only one subscript and such a variable is called a one-dimensional array.
Declaration of one-dimensional array:-Arrays must be declared before they are used so that the compiler can allocate space for them in memory. The general form of array declaration is:

The type specifies the type of element that will be contained in the array, such as int, float, or char and the size indicates the maximum number of elements that can be stored inside the array.
For example: float height [50];
Initialization of one-dimensional arrays:-After an array is declared, its elements must be initialized. Otherwise, they will contain "garbage value". An array can be initialized at either of the following stages:
Compile time initialization: We can initialize the elements of arrays in the same way as the ordinary variables when they are declared.
The general form of initialization of array is:

Runtime initialization: An array can be explicitly initialized at runtime. This approach is usually applied for initializing large arrays.
For example, consider the following segment of a C program:

2.Two-Dimensional Array:-

In a normal array, data is stored in the form of a list which contains one element after another.  If you want to store data in tabular form, then you can create a two dimensional array for this.
If you want to store 4 employees id and their phone numbers. The table for this will be created as follows:
111.            123456
112.            123457
113.            123458
124.            123459
This type of table can be created in memory by a two dimensional array.
Declaration of two-dimensional array:- Two-dimensional arrays are declared as follows:

Two-dimensional arrays are defined in much same manner as one dimensional array, except that a separate pair of square bracket is required for each subscript. Thus, a two-dimensional array wil require two pairs of square brackets. Two-dimensional arrays are stored in memory.
Initialization of two-dimensional arrays:- Two-dimensional array may be initialized by following their declaration with a list of initial values enclosed in braces.
For example, int table (2] [3] = (0,0, 0, 1, 1, 1,); initializes the elements of the first row to zero and the second row one. The initialization is done row by row.

3.Multi-dimensional arrays:-

C allows arrays of three or more dimensions. The exact limit is determined by the compiler. The general form of a multi-dimensional array is :
type array_name [s1] [s2] [s3] is the size of the dimension.
Some examples are:
int survey [3] [5] [12] ;
float table [5] [4] [5] [3] ;

Post a Comment

0 Comments