Arrays are the collection of fixed size , same type of elements in a sequential order. These elements are basically variables of same data type . They can be   acessed via indexing i.e they are alloted some number through which each element in an array can be acessed .

Each element takes a memory block to store its data in it .Similary array also takes many memory blocks to store its data depending upon its arraysize  i.e

number of memory blocks allocated=array size 

Consider the following figure :

array 4.2

 

in this way memory blocks are arranged , and the size of the memory block depends upon the data type . Like if data type is int then it will take 4 bytes and so on …

Here these 10 blocks reprents the array size which could have been more than that or less than 10 . It all depends upon the size taken by the programmer.

SYNTAX

datatype name_of_the_array[arraysize];

here datatype can be build in type like  int, char, float, etc or user defined type about which we’ll discuss later .

so , lets continue now comes the array size , this is a numeric value and tells about the size of the array i.e how many elements can be inserted in an array .

Now the question that maximum of you might have ……

Can the size of an array  be negative????

You can even make the user decide what should be the size of the array .Remember the size of the array tells the compiler how many blocks of the memory should be reserved for that particular array .

consider the following example , this will help you to undersatnd better …

array 4.1

 

clearly you can see that when a negative size was used then so many errors where generated .

NOTE

Never ever take size of an array as negative . In c++ its not allowed to have a negative size .

 

 

The rest of the topic is discussed in my next post .

NO COMMENTS