C Programming language

adplus-dvertising
What is an Array ?
Previous Home Next

An Array may defined as the collection of the homogeneous (same type) elements that are stored in the contiguous memory locations. For example, if we have to store 7 values of any type, lets say, integer values we don't have to declare 5 variables with different identifiers but we can define an array of integer type and can store the values in array directly with a unique identifier. Thus, an array named arr created in memory having 5 values can be represented as :

Element12345
Index01234

Cells have been assigned values from 0 to 4 as in arrays values are stored from a base index of 0.

How to declare an Array ?

An Array can be declared with the help of an unique identifier specifying its data type and also by specifying the number of elements it must contain. The number of elements are declared inside a square bracket i.e. []. Thus when an array of integers having 5 elements considering the above figure can be declared as :

int arr[5];   /*the element field within the [] should be a constant value as the are the blocks of static memory whose size must be declared before execution. For variable length array dynamic memory is required which we will discuss later.*/

How to initialize an Array ?

While declaring the array for a local scope e.g. for a particular function its contents will not be initialized by default and will remain undetermined till we initialized it separately. In case of array of global scope the values are initialized but having the default values of 0's for each element.

Thus, for the array of both scopes we can initialize an array by enclosing the elements within the {} brackets. For example,

intarr[5] = {1,2,3,4,5}; /* This array will be created as which one is shown in the above figure. */

The no of elements declared within the {} must not exceeds the numbers of elements defined in the [], else it could some serious trouble to your program.

What are the advantages of using an Array ?

Following are main advantages of using an array :

  1. Arrays are the most convenient way of storing the fixed amount of data that can be accessed in an unpredictable fashion
  2. Arrays are also one of the most compact data structures as in for storing the 50 elements in an array of integer type it will take the memory equivalent to the amount of memory required to store 50 elements in addition to that a few overhead bytes for whole array.
  3. Another advantage of array is that iterating through an array is more faster than compared to other data structure types like linked list because of its good locality of reference.

What are the disadvantages of using an Array ?

Following are the main disadvantages of using an Array :

  1. Performing various operations like insertion and deletion is quite a stiff task in arrays as after inserting or deleting an item from an array every other element in array have to move forward or backwards prior to your operation, which is a costly affair.
  2. In case of the static arrays the size of the array should be know up priori i.e. before the compile time.
  3. C language does not have a checking mechanism for the array sizes.
Previous Home Next