Array definiation and syntax in detail

An array is a collection of homogeneous or similar kind of data types in contiguous memory location. Each element in the array is referred to as the array element of the array. The array elements are accessed by using an index number which represents the position of an element in the array.


In general the index of an array with n elements begins from zero and ends at n-1. For instance consider an array named Score which contains 10 elements. The index of the elements range from 0 to 9. The following depicts the above example.

array definition, syntax with example


Array is a reference type data type. It is due to the fact that when an array is declared, the starting index (0th position) of the array is assigned in a memory location. The elements following the starting index are stored in the next consecutive memory locations. With the help of the memory address containing the starting element and the size of the array, we can access the complete array. Let’s elaborate it with the help of the above illustration:

Suppose the 0th position of the Score array is stored in a memory address 2500. The next elements of the array i.e. the data in the 1st position can be found in the location 2501 and so on… This continues till the last index of the array. The 10th element or the 9th position data will be stored at 2509.

Declaration of an Array:

In C language or C++, an array needs to be declared along with its size before it can be used. The basic syntax for its declaration is as follows:


DataType ArrayName[size];

DataType:

It is used to specify the particular data type which will be stored in the array. DataType like int, double, float, char can be used. In C or C++ we cannot declare a string as such. But in order to work with sting, we make use of a continuous sequence of characters in form of an array of char dataType.

ArrayName:

It is used to specify an array with a unique name. Any well-defined identifier acts as an ArrayName.

[size]:

The size of the array is declared in between the [ ].
Example:
int Score[10];


This declares an Array named Score with 10 elements as explained in example above.

Initializing an array:

There are different techniques in which values can be assigned to an array. In general the initialization of arrays takes place at run time. The prominent ways to initialize an array is explained below:

Using Index location:

In a pre declared array int Score[10], we assigns a value 123 to the first element of the array.

Score[0] = 123;

Using both size and values:

Int Score[5] = {27, 87, 73, 57, 93};
The number of values or elements between the braces {} cannot be larger than the size of the array.

Using values only:

Int Score[ ] = {27, 87, 73, 57, 93};
An array of size depending on the number of values in the {} is initialized at run time.

Accessing an array:

The elements of an array can be accessed using the index of the array.

int value = Score[4];

The variable value will hold a value 93. An Array OverFlow exception may occur if the index stated is Out of the Range of the array.

Operations performed in an array:

The following operations can be performed in an Array:
• Insertion
• Deletion
• Searching
• Push and Pop in Stacks
• Enqueue and DequeueQueues

Disadvantage:

An array can hold data of a particular data type only.

Advantage:

Element at any position can be retrieved using the index of the array.

Comments

Popular posts from this blog

Samsung Galaxy Note 3 - Full Specifications

Run Borland Turbo C/C++ in Android Devices