Sample Essay

Words 1,439


An array is considered a crucial component for any large program which requires storage of a large number of variables of same type. The use of array not only allows the programmers to manage the variable in an efficient manner but also reduces the amount of code written for a program. To effectively use an array, it is important for a programmer to know how it can be allocated and initialized. This is discussed in the following lines.

Allocation of an Array

An array can be allocated in a program in two ways, which are static allocation and dynamic allocate. In static allocation of an array, the size of array is usually known, hence it involves the declaration of the name of array along with its type and size.. The size of the array is usually mentioned using braces. Multidimensional arrays are declared using multiple braces. Indexes are mentioned within the braces when an array component is accessed. The following code shows the static allocation and use of array.

int arrayone[10];   // 1 dimensional array

int arraytwo[10][10];  // 2 dimensional array

int arraythree[10][10][10];  // 3 dimensional array.

int one, two, three;

one = arrayone[2];   // index mentioned within the braces

two = arraytwo[2][3];  // access component at 2nd row and 3rd column

three = arraythree[2][3][4];

Dynamic allocation array is done using dynamic allocation function, which in case of C/C++ is malloc(). malloc() is used to allocate space equivalent to the size of the required array and the array is accessed with the help of pointers. Arrays having more than one dimensions require allocation of N one-dimensional arrays which are linked together with array(s) of pointers (Hermetic Systems, 2010).

Initializing an Array

Static arrays can be initialized at the start of a program. For example


int one[10]= {1,2,3,4,5,6,7,8,9,10};

This initializes the array to numbers from 1 to 10. For larger arrays size , a for loop can be used to initialize all components of an array. For example


int one[10];



for(int i = 0; i<10; i++)

one[i] = 100;


Dynamic arrays can only be initialized after they have been allocated in the code. for loop can be used for initializing as well. For example

Kindly order term papers, essays, research papers, dissertations, thesis, book reports from the order page.


Related Pages

Tags: ,