Understanding Arrays:

The array is a collection of the same type of data.

  • An array is a collection of data storage locations, each having the same data type and the same name
  • The first element in the array is numbered 0, so the last element is 1 less than size of the array.
  • An array is also known as a subscripted variable, as elements of array are accessed using[1], ar[5]
  • Before using an array its type and dimensions must be declared.
  • However big an array, its elements are always stored in contiguous memory locations.

Arrays Declaration and Initialization:

Array Declaration:
int age[5];

Here int specifies the type of variable and ‘age’ specifies the name of the variable. The number 5 tells how many elements of type int are there in our Array. The bracket [] tells the compiler that we are dealing with array.
When you declare an array, you can specify the number of elements with a literal constant or with a symbolic
constant created with the #define directive. Thus, the following:

#define MONTHS 12
int array[MONTHS];
is equivalent to this statement:
int array[12];

  • With some compilers, however, you can’t declare an array’s elements with a symbolic constant created with the

const keyword:
const int MONTHS = 12;
int array[MONTHS]; /* Wrong! */

Array Initialization:

  • You can initialize all or part of an array when you first declare it. Follow the array declaration with an equal signand a list of values enclosed in braces and separated by commas. For example, the following code assigns the value100 to array[0], 200 to array[1], 300 to array[2], and 400 to array[3]:

int array[4] = { 100, 200, 300, 400 };

  • If you omit the array size, the compiler creates an array just large enough to hold the initialization values. Thus, thefollowing statement would have exactly the same effect as the previous array declaration statement:

int array[] = { 100, 200, 300, 400 };

  • You can, however, include too few initialization values, as in this example:

int array[10] = { 1, 2, 3 };

  • If you don’t explicitly initialize an array element, you can’t be sure what value it holds when the program runs.
  • If you include too many initializers (more initializers than array elements), the compiler detects an error.
  • Suppose, you declared the array of 10 elements. For example: ar[10]. You can use array members
    from ar[0] to ar[9]. But, what if you want to use element ar[10], ar[12] etc. Compiler may
    not show error using these elements but, may cause fatal error during program execution.


Mr. Sandeep Soni

Founder, Trainer & CEO, Deccansoft Software Services.

Sandeep has 21 yrs of experience working in various Microsoft Technologies/Platforms incl. VB6.0, ASP, VC++, VB.NET, C#. He is involved in managing and architecting projects at Deccansoft. He will be your liaison to Deccansoft, for any kind of communication and project updates. He knows what works and what doesn’t, and what practices are most suitable for design and programming with the ultimate goal of producing a quality system.