Ads block

Banner 728x90px

Arrays

This chapter includes the introduction of array, declaration of the arrays and some other points that's need to know about the arrays.

Introduction

An array is a collection of identical data objects which are stored in consecutive memory locations under a common variable name.

An array can be defined as a indefinite collection of homogeneous(similar type) elements. This means that an array can store either all integers, all floating point numbers, all characters or any other complex data type but all of same type.

There are some important points to be pointed out about arrays.

  • Arrays are always stored in consecutive memory location.
  • An array either be a integer, character, or floating data type can be initialized only during declaration time, and not afterwards.
  • An array can store multiple values under a common heading or variable name.
  • Array name is actually a pointer to the first location of the memory block allocated to the name of array.
  • There is no bound checking concept for arrays in C.

Array Declaration

Before any linear or multidimensional array is used in a program, one must provide to the compiler the following information.


  • Type of the array
  • Name of the array
  • Number of subscripts in the array (whether the array is 1Dim or 2Dim etc.)
  • Total number of memory locations to be allocated.

In general, one dimensional array may be expressed as

Syntax

storage_class data_type array_name [expression]

Where the storage_class refers to the scope of the array variable such as external, static, register and an automatic.

 data_type is used to declare the nature of the data elements stored in the array, such as(character type, integer and floating point).
Array_name is the name of the array, and expression indicates size of the memory locations required for further processing.


The storage_class is optional. Automatic storage_class comes by default with the array when it is defined in the function and outside the function.


Some valid one dimensional array declaration are:

Examples

int marks [10];
char name [15];
float value [20];