Testing.

Sunday, 5 February 2017

C programming language: What is an Array in C ?


An array is a derived data type in 'C' which is constructed from fundamental data types of 'C' programming language.

An array is a collection of same types of data elements in a single entity.

In implementation when we require 'n' numbers of values of same data type, then recommended to create an array.

When we are working with arrays always static memory allocation will happen i.e. compile time memory management.

When we are working with arrays, always  memory   is constructed in continuous memory location that's why it is possible to access the data randomly.

When we are working with arrays all values will share same name with unique  identification value called 'index'.

And this array index must be started with 0 (Zero) and ends with (size-1).

It is required to use subscript operator '[]' while working with arrays.

Subscript operator '[]' require one argument of type  Unsigned integer constant, and its value must be ' > 0'  (greater than)only.

Syntax to create an array:-
                             DataType ArrayName[size];
         Example:-
               int arr[10];
               char arr[10];

In general arrays are classified into two types:
1) Single -dimensional arrays.
2) Multi dimensional arrays.

1) Single-dimensional arrays:-
                A single dimensional array and one-dimensional array consist of a fixed number of elements of the same data organized as a simple linear sequence. The elements of a single-dimensional array can be accessed by using a single subscript operator, thus they are also known as single sub-scripted variable.and it is also known as linear arrays and vectors.
     Single dimensional array is shown as below:-



 Declaration of a single-dimensional array:-
                The general form of a single-dimensional array declaration is 
                                 DataType ArrayName[size];
Example of single-dimensional array:-
                   int arr[5]; //array of 5 integers.
                   float arr[5];  //array of 5 floats.
                   char[10]; //array of 10 characters.

2) Two-dimensional arrays:-

 In 2D array, elements are arranged in rows and column format.

When we are working with 2D array , we required to use 2 subscript operators which indicates row size and column size.

The main memory of 2D array is row size and elements are available in column.

On 2D array when we are applying one subscript operator then it gives row name, row name always provides corresponding row address.

From 2D array when we required to access the element then 2 subscript operators required to use.

Array name always provides main memory address of an array, 

  Two-dimensional array is shown as below:-

Declaration of a two-dimensional array:-
                The general form of a two-dimensional array declaration is 
                                 DataType ArrayName[Rsize][Csize];
               where Rsize indicates row size and Csize indicates column size.
Example of two-dimensional array:-
                   int arr[2][3]; // integer array of  2 rows and 3 columns.
                   float arr[5][1];  // float array of  5 rows and 1 columns.
                   char[3][3]; // character array of  3 rows and 3 columns.

No comments:

Post a Comment